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'
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With