# Diff between redirect\_to :action and calling action/render?

**URL:** <https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937>\
**Category:** Rails\
**Created:** [October 12, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937 "2006-10-12T14:46:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [October 12, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937/1 "2006-10-12T14:46:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [October 12, 2006, 3:04pm UTC](https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937/2 "2006-10-12T15:04:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [October 12, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937/3 "2006-10-12T15:32:48Z")

</div>

Thanks for the quick response, Frederick.

soooo…

In first case the URL in the browser will say:

[http://site.com/controller/list](http://site.com/controller/list)

and in the second it will say:

[http://site.com/controller/index](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

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [October 12, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937/4 "2006-10-12T15:43:59Z")

</div>

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](http://site.com/controller/list)
> 
> and in the second it will say:
> 
> [http://site.com/controller/index](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

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [October 12, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937/5 "2006-10-12T15:42:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [October 12, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937/6 "2006-10-12T20:00:35Z")

</div>

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

c.

---

<div class="post-metadata">

**Author:** ![yardboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/9de0a6/32.png) [@yardboy](https://www.ruby-forum.com/u/yardboy)\
**Post date:** [October 12, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/diff-between-redirect-to-action-and-calling-action-render/71937/7 "2006-10-12T16:16:52Z")

</div>

Cayce B. wrote:

> [http://site.com/controller/index](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)
