MarkUs Blog

MarkUs Developers Blog About Their Project

render vs. redirect_to in Ruby on Rails 3

with 7 comments

Here’s what you need to know when you’re deciding between a render and a redirect_to in your controller action:

First, what do render and redirect do?

render will render a particular view using the instance variables available in the action. For example if a render was used for the new action, when a user goes to /new, the new action in the controller is called, instance variables are created and then passed to the new view. Rails creates the html for that view and returns it back to the user’s browser. This is what you would consider a normal page load.

redirect_to will send a redirect to the user’s browser telling it to re-request a new URL. Then the browser will send a new request to that URL and it will go through the action for that URL, oblivious to the fact that it was redirected to. None of the variables created in the action that caused the redirect will be available to the redirected view. This is what happens when you click on ‘Create’ in a form and the object is created and you’re redirected to the edit view for that object.

So which one should you use?

If you’re lucky, neither! If you don’t have a render or a redirect_to in your controller method for a particular action then Rails will automatically render the template for that action. For example if your action is new, rails will render the view for new: new.html.erb.

If you need to render another action’s view, you can use render :action_name. For example if the user has filled in a form at /new and they click ‘Create’, the form is posted to the create action. If there are errors with creating the object, in the create action you can use render :new to show the same form as before but with the error messages for why the object failed to be created. These errors can be displayed because the new template will have access to the variables in the create action. If you used redirect_to here, you wouldn’t be able to show the errors and anything typed into the form would be lost.

If you need the user to run a new action, use redirect_to :action => :action_name. For example if the user has clicked ‘Create’ and there are no errors in creating the object, you can use redirect_to :action => :show to show the successfully created object.

 Here’s an example of the correct uses for an imaginary Book controller:

#renders the 'new' view automatically

def new

  @book = Book.new

end

def create

  @book = Book.new params[:book]

  if @book.save

    redirect_to :action => :show #if the book is successfully created,

                                 #redirect to its show action

  else

    render :new #else render the new action again but with error messages available

                #from @book.errors and with the fields the user filled in available

  end

end

 

A small note on syntax:

Before Rails 3, to render a different action you would have to use render :action => "new" but now you can just use the symbol for the action: render :new

See http://guides.rubyonrails.org/layouts_and_rendering.html for more information.

Written by lkysow

November 16th, 2011 at 4:33 pm

7 Responses to 'render vs. redirect_to in Ruby on Rails 3'

Subscribe to comments with RSS or TrackBack to 'render vs. redirect_to in Ruby on Rails 3'.

  1. Hello, I think your website might be having browser compatibility issues.
    When I look at your blog site in Safari, it looks fine but when opening
    in Internet Explorer, it has some overlapping. I just
    wanted to give you a quick heads up! Other then that, awesome blog!

    Virtual

    1 Oct 14 at 5:26 am

  2. Good post, but I do have a comment though. With render you can also render other actions so for the case of create, after controller create a post it can render to edit action just that before it renders to edit it needs to define variable that edit will use and it will work the same as redirect_to. So it’s still not clear why you would prefer using redirect_to over render.

    Antonio

    5 Nov 14 at 7:43 am

  3. Pretty! This has been an extremely wonderful article.
    Thank you for supplying this information.

    back spasms

    6 Nov 14 at 2:21 pm

  4. Antonio, can you explain your comment?

    Hitesh

    10 Dec 14 at 12:43 pm

  5. Good post! We will be linking to this great content on our website.
    Keep up the good writing.

    Ciekawe filmy

    31 Dec 14 at 2:49 pm

  6. thanks, helpful..
    go ahead

    jokowow

    13 Jan 15 at 12:05 am

  7. I am no longer certain where you’re getting your information, however good topic.

    I needs to spend a while learning more or understanding more.
    Thanks for great info I used to be looking for this info for my
    mission.

    eagine-pro

    19 Jan 15 at 6:53 am

Leave a Reply