views:

161

answers:

3

In a Rails app sometimes you use a redirect in an action...

redirect_to :controller => 'sessions', :action => 'new'

I wonder if that's bad though because it sends back a 302 status to the browser and then the browser makes a whole new request. It's an additional back-and-forth.

Would it be better to just render a template?

render :template => 'users/new'
+10  A: 

The main reason for using redirects instead of renderings is to ensure the idempotent invariant. This basically mean that if you modify something from a POST or DELETE, then you should redirect to the next page. Otherwise, if someone tries to update, they might redo the mutating operation. It also makes it easier for the user since they can always bookmark a specific page. That is not necessarily true if you have used a POST to get to the current place.

But yes, it will be mildly less efficient - although in this case I would care more about usability of the application.

Ola Bini
+4  A: 

In addition, it won't be that much less efficient, because HTTP 1.1 keep-alives should ensure the browser can re-use the same connection to make the second request, rather than having to start again.

bobince
A: 

Just as an addendum. When it comes to mobile devices, reducing the number of requests is huge. There's added overhead for each request, probably because the device fires up the wireless communication stack each time it does a request, to save battery. So every little thing helps.

I'm working on a large web application. Once we started using CSS Spriting we saw a huge increase in loading speed. A much larger speed gain than we thought we'd get.

Hans Sjunnesson