HTTP 302 and redirect_to

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.

On 11/23/06, Sam [email protected] 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
http://www.ElectroInteractive.com

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs