Diff between redirect_to :action and calling action/render?

I’ve seen these two constructs used in different places and was
wondering if someone could explain the differences in use, ie why one or
the other in specific situations…

def index
redirect_to :action => ‘list’
end

or

def index
list
render :action => ‘list’
end

thanks.
c.

redirect sends back a response to the browser telling it to load a new
url instead of the one requested, whereas render doesn’t(since it just
changes the template used

Fred

Thanks for the quick response, Frederick.

soooo…

In first case the URL in the browser will say:

http://site.com/controller/list

and in the second it will say:

http://site.com/controller/index?

But otherwise the net result of what code is processed and what content
is displayed is the same?

Thanks.
c.

Frederick C. wrote:

redirect sends back a response to the browser telling it to load a new
url instead of the one requested, whereas render doesn’t(since it just
changes the template used

Fred

Cayce B. wrote:

Thanks for the quick response, Frederick.

soooo…

In first case the URL in the browser will say:

http://site.com/controller/list

and in the second it will say:

http://site.com/controller/index?

that is correct.

But otherwise the net result of what code is processed and what content
is displayed is the same?

That should normally be the case.
An example of when you would want to do a redirect rather than a render
would be if you had an an action that added something to your list: if
you do a render then if the user does a refresh then they will call your
add action, whereas if you redirect to the list action in the add action
then refreshing the page will just show the list again, rather than
calling add again.

Fred

Some of the consequences of using render is that the browser’s url
remains whatever what was used to generate the request. redirect,
since it asks the browser to generate a new request, has the side
effect of changing the browser’s url. This allows you to do things
that are more resilient to users using the back button or hitting
reload. The pattern is called “Redirect after Post” and you can
google it to find out more details.

In short, if you’ve done some kind of operation that changes the
state of the system, such as updating or creating a model object, you
should redirect to different page. Thus, hitting reload on the
browser won’t try to update the system again.

Hope it helps,

-Anthony

Things are coming very clear - thanks to everyone for input.

c.

Cayce B. wrote:

http://site.com/controller/index?

But otherwise the net result of what code is processed and what content
is displayed is the same?

Thanks.
c.

…uhm… not exactly.

  • redirect_to makes the browser load a new page, and the result is the
    same as if the user manually nagivated to this URL or entered it in the
    address field of his browser.
    so to stick to your example: once the browser has requested the new
    page he has been redirected to (controller/list), the “list” action
    will be executed, and then the corresponding view will be parsed and
    filled with the data made available to it in the action.

  • render will only render the view of the “list” action (list.rhtml)
    without calling the “list” action first. So you would have to make some
    data availabe in the index action that can be parsed in the “list” view
    file, which would not be very DRY (Don’t repeat Yourself)