#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0

    Rails 'render' question


    Is it possible to render a template for an action not within the current controller?

    e.g.,
    Code:
    render :controller => 'different', :action => 'index'
    If there is both controller_name.rhtml and application.rhtml within the layouts directory, can I specify which layout to use (or alternatively render them within eachother)?
  2. #2
  3. $foo->bar($_);
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Location
    London
    Posts
    86
    Rep Power
    54
    You can access any actionview using
    Code:
    render :template => 'different/action', :layout => 'mylayout'
    The layout is optional. Normally the current layout is used.

    Usually you create a partial view, when you want to reuse some of your code in different actions. It is a much cleaner approach.

    Code:
    # render $RAILSROOT/app/views/lib/_adminheader.rhtml
    # using layout 'layouts/thelayout.rhtml'
    # through :locals we provide the variables used in the partial
    render :partial => 'lib/adminheader',
           :locals => { :page => @page, :somevar => @variable },
           :layout => 'thelayout'
    Take a look here for further documentation.

    bye
    grande
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0
    Perfect, thanks!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Posts
    4
    Rep Power
    0
    OK, one more question:

    If I call
    Code:
    render :template => 'different/action'
    This will not execute the controller code for the called action, but will only render the view of that action (using locals, etc. from the currently executing action).

    OTOH, if I call
    Code:
    render_component :controller=> 'different', :action => 'action', :params => params
    This both executes the controller action for 'different/action' and renders the view of that action (using locals, etc. from the 'different/action' action.)

    Is that correct?

IMN logo majestic logo threadwatch logo seochat tools logo