redirect_to or render

1,987 views
Skip to first unread message

shaish

unread,
Feb 28, 2008, 5:21:20 PM2/28/08
to Ruby on Rails: Talk
Hey, a newbie here.
Can someone please give me a brief explanation whats the difference
between the redirect_to method and the render method and when should I
use one instead of the other ?
thnx alot !

Greg

unread,
Feb 28, 2008, 5:25:25 PM2/28/08
to Ruby on Rails: Talk
redirect_to will issue a 302 to the browser, so that the browser will
issue a new request.

render will create content. You have to make sure that your
controller has set up all of your variables for any render that you
create.

If you are going to cross controllers, or you do not have enough
information to call a particular render, then you want to use
redirect_to. If you choose between rss/html or one layout vs.
another, then you can use render.

s.ross

unread,
Feb 28, 2008, 5:26:42 PM2/28/08
to rubyonra...@googlegroups.com

Use redirect_to when you want the browser to receive a 302 redirect
response and go to another URL. Use render when you don't need the
browser to issue another request, but can render a page and return a
200. Examples:

- User fills in form in new action
- Fields are filled in incompletely or incorrectly
- Post goes to the create action, which understands that and renders
the 'new' template again with a 200 so the user can fix errors

- User fills in form in new action
- Fields are filled in correctly
- Post goes to create and information is successfully added to the
database
- You issue a redirect to the index page (or something)

Reply all
Reply to author
Forward
0 new messages