Rails Render

Rails Render is a strong method that helps you to render your web pages. It comes with many flavors including:


1) Render action: render an action in the current controller, you can specify if you want layout printed or not

2) Render partial: renders part of your web page

3) Render template: render a page, the file path is relative to your application

4) Render file: absolute path is needed

5) Render text

6) Render Json


These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blinkbits
  • BlinkList

7 Comments »

  1. Mitchell Gillespie said,

    November 27, 2007 @ 11:59 pm

    Hi,

    Is it possible to render an action from a different controller?? if in my routes.rb file I have.. map.connect ‘:controller/:action/:id’ could I not do a:

    render :action => ‘//’ ???

    Thanks,
    Mitch

  2. adam said,

    December 25, 2007 @ 6:15 am

    you can just do:

    render :controller => ‘controller_name’, :action => ‘action_name’

    should do the trick!

  3. Rails Render | Ruby on Rails Türkiye said,

    February 7, 2008 @ 7:36 am

    [...] Kaynak: http://www.rubyonrailsexamples.com/rails-tips/rails-render/ Ekle Sosyal link paylasim sitelerindeki hesabiniza ekleyip, daha sonra kolaylikla bu yaziyi [...]

  4. vatti said,

    June 24, 2008 @ 4:08 am

    hi,

    can u plz tel about difference between render & redirect action..

    thanks

  5. Ben Ridout said,

    July 21, 2008 @ 8:05 am

    Vatti,

    There are a few differences between render and redirect.

    HTTP is based on a request/response model. When a user clicks a link or submits a form, your controller handles the request and decides how to process it and what response to send back to the browser. Normally, the response is just a view template. Typically, wou would call render :action=>’foo’ (or rely on the default behavior) to return a view template as the response to the request.

    Redirect_to works differently. When you call redirect_to, the response to the initial request is an instruction to the browser to initiate a second request. Instead of rendering a view template for the user, the browser is told to request a second page/uri. Then that request is handled like any other HTTP request, and it must return an HTTP response. This second response can be a view or it could even be another redirect. With redirect, the user’s browser will make to separate calls to your application. Any parameters from the first request will be lost, and any state in your app tied to the request, like the flash, will also lost when you are processing the 2nd request.

    Also keep the following in mind:

    By default, the view with the same name as the current action/controller will be rendered. So if you have a controller named ‘User’ and it is processing a request to the ‘edit’ action, the ‘edit’ view will be rendered by default.

    Specifying render :action=>’foo’ explicitly gives you the opportunity to override the default behaviour and render a different view. Chaging :action allows you to change among views for the current controller. You can also render a view fro ma different controller and you can pass in optional params like :id. You are just telling Rails what view to display in response to the current request. The controller (and additional params) are optional. The current controller is assumed by default..

    From your controller, you can render any view template for that controller:
    render :action=>’foo’
    as well as view templates from other controllers:
    render :action=>’bar’, :controller=>’some_other_controller’

    Render :action is misleading because you are not telling Rails to call a controller’s action method. You are just telling it to render the view with the same name as an action. It means: render the view associated with action ‘foo’.

    If you are in the ‘edit’ action in your controller and you render :action=>’show’, the show.rhtml template will be rendered, but the show method/action in your controller will not be executed. Render implies view/display, not execute a controller action.

    If you actually want to run another action, you can call one action from another in your controller, but there is a danger to this if both actions render a response. You can only return one HTTP response per request. If you create 2 (one from each action) you will generate an exception.

  6. prash said,

    March 17, 2009 @ 9:20 am

    thanks ben!

    “Render :action is misleading because you are not telling Rails to call a controller’s action method. You are just telling it to render the view with the same name as an action. It means: render the view associated with action ‘foo’.
    If you are in the ‘edit’ action in your controller and you render :action=>’show’, the show.rhtml template will be rendered, but the show method/action in your controller will not be executed. Render implies view/display, not execute a controller action.”

    the most important lines of your very useful comment. i had wanted to actually run the action first and then get to the view but could just not figure out why it was not entering the action even though i could plainly see that the view for that action was being rendered.

    now i realize that the action is never executed and only the view is called! so i used redirect_to instead (in my case it was ok to call that, but i wonder how to solve this problem when you don’t want a browser redirect )

    one more question:
    you have mentioned the “multiple render” problem. is this problem only there if there is an explicit call to render in both actions or even when it is implicit (as is default: rails will call the default view for an action after execution of an action)

    thanks!

  7. Rails: redirect_to vs. render « helloworlder said,

    April 15, 2009 @ 3:09 pm

    [...] http://www.rubyonrailsexamples.com/rails-tips/rails-render/ <– look in the comments for the author called Ben Ridout [2] [...]

RSS feed for comments on this post · TrackBack URI

Leave a Comment