# HTTP 302 and redirect\_to

**URL:** <https://www.ruby-forum.com/t/http-302-and-redirect-to/77079>\
**Category:** Rails\
**Created:** [November 24, 2006, 3:48am UTC](https://www.ruby-forum.com/t/http-302-and-redirect-to/77079 "2006-11-24T03:48:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@sam](https://www.ruby-forum.com/u/sam)\
**Post date:** [November 24, 2006, 3:48am UTC](https://www.ruby-forum.com/t/http-302-and-redirect-to/77079/1 "2006-11-24T03:48:08Z")

</div>

Hi. I have a basic question here. How exactly does a redirect\_to (HTTP  
302) work? If I have a redirect\_to(URL), does it basically send back an  
HTTP 302 back to the client, as in browser and then the browser asks for  
the redirect\_to URL? Or, do the redirection happen on the web server  
without going back to the client? I’m a little confused here because I  
would imagine that a redirect\_to would slow things down.

Say I have a login controller and a user controller. If a user signs  
in, I have them execute the login/login controller/action and if that is  
successful, I will redirect\_to them to an action in the user/index  
controller/action. If the redirect\_to causes another roundtrip,  
wouldn’t it be better to just execute the user action and template in  
the login controller?

Thanks for your help.

---

<div class="post-metadata">

**Author:** ![sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@sam](https://www.ruby-forum.com/u/sam)\
**Post date:** [November 24, 2006, 4:30am UTC](https://www.ruby-forum.com/t/http-302-and-redirect-to/77079/2 "2006-11-24T04:30:42Z")

</div>

On 11/23/06, Sam [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in, I have them execute the login/login controller/action and if that is  
> successful, I will redirect\_to them to an action in the user/index  
> controller/action. If the redirect\_to causes another roundtrip,  
> wouldn’t it be better to just execute the user action and template in  
> the login controller?
> 
> Thanks for your help.

redirect\_to does indeed send a 302 response to the browser, which causes  
the  
browser to make a new request at the redirect URL. A whole new web  
request  
occurs, and none of the code executed before the redirect is retained in  
memory.

–  
Scott B.  
Electro Interactive, Inc.  
Office: 813-333-5508

> **[Electro Interactive - Web Development. Ruby on Rails. Web Standards.](http://www.electrointeractive.com)**
>
> Electro Interactive is a full service new media design and development company.

---

<div class="post-metadata">

**Author:** ![sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@sam](https://www.ruby-forum.com/u/sam)\
**Post date:** [November 24, 2006, 4:44am UTC](https://www.ruby-forum.com/t/http-302-and-redirect-to/77079/3 "2006-11-24T04:44:07Z")

</div>

The redirect sends back as part of the response a Location header that  
tells the browser where to go.

You could just call the appropriate methods, render the right template  
and so on, but that’s not really a good idea: The browser still thinks  
its addressing the original resource, so reload, back and so on won’t do  
the right thing:

Common example: On a blog you submit a comment, and are redirected to a  
page showing the comments. If you hit refresh, you just see the comment  
list again. If you had just done a render, then the browser thinks it’s  
still on the comment submission page =\> refreshing would submit again.

Fred
