RailsCasts Pro episodes are now free!
Learn more or hide this
Useful as usual.
Good to see you still make simple Railscast suited also for Rails-beginners.
Wanted to learn a bit more about the 'request' method/object and what you can do with it. But the docs don't have much to say about it. Do you know where it is defined and what else you can do with it?
Thanks for these episode. Really helpful tip.
Thanks for your Railscasts. I am not sure if its a sign of how sad my life is at the moment but I look forward to seeing the new Railscast every monday. I used to get Crash magazine as a kid and my little heart would beat faster when I remembered that was the day that it was in the shops. Get the same feeling revery monday when I see the RSS feed, I am a newbie and much goes over my head but heh. Do I need help?
Well done & thanks.
I define a redirect_back_or_default method that uses redirect_to(:back) and ActionController::RedirectBackError: http://pastie.textmate.org/private/2pc1znldyvmbencfpvf0g
…since redirect_to(:back) will raise if there is no refer(r)er. I don't think you said explicitly.
Trying the link again: <a href="http://pastie.textmate.org/private/2pc1znldyvmbencfpvf0g">http://pastie.textmate.org/private/2pc1znldyvmbencfpvf0g</a>
Wonderful screencast. This is one of those little things everyone needs to do but not everyone knows the best approach. Thanks for continuing to do the quick simple tips between the heavy hitter subjects!
@Richard: Your not alone, its about the only thing to look forward to on Monday! :)
@Per Velschow, the request object is documented under ActionController:AbstractRequest which you can find here:
The "env" variables (like I'm using here) can change per request. Try tossing this in a view to see the options:
<%= debug request.env %>
@Henrik, Good point! I forgot to mention the :back option will raise an exception if the referer isn't specified.
What about the scenario of linking to an edit form, i.e. /products/5/edit, and after submitting it goes to /products/5/update. Redirecting back from here goes back to the edit form. I've used a few different solutions (check points, passing original_uri GET parameters, etc)
Anyone else have this issue or want to share a solution?
@Austin, You can use the referer technique for this, but you'll have to capture it when entering the form instead of when the form is submitted. Something like this:
<%= hidden_field_tag :referer, (params[:referer] || request.env['HTTP_REFERER']) %>
Then you can use params[:referer] in the controller to redirect back.
In version 2.2, there's a new 'referrer' method in request.rb. This means you can simply use "request.referrer" in the controller.
Another way to attack this problem is to have the User's URL store their previous route.
It's not ideal for every case, but if you have a lot of this stuff going on on your site (especially with polymorphic-like views), it can be handy as you don't have to manually keep tabs on the referer.
I've been playing around with putting this into a plugin for the last few weeks at:
It's not totally weaponized yet, but I think the concept is sound.
Very usefull cast!
Since the referrer is unreliable (I always turn off sending it in my browsers), I'd suggest storing request.request_uri in session instead and using that when necessary.
@AC, that's a cool idea. But don't you need to use an after_filter instead of a before_filter so it resets at the end of the request instead of the beginning?
Also you can make exceptions using the skip_after_filter method in the controller.
Nice Tip :-)
Very useful little screencast again
please also mention the most obviouse choise:
link_to 'Continue', :back
This has nothing to do with this Railscast, but I have a suggestion for a Railscast topic.
Would you please do a Railscast on the usage of html frames w/in Rails, particularly if the contents of those frames are dynamic. I'm not sure that this is even possible, but I would love to know for sure.
I think I'll choose for not using the session, but have the browser keeping state by appending the continu shopping url to the show cart URL.
Thanks! A simple technique and a great tutorial.
We have to keep in mind that request.env['HTTP_REFERER'] doesn't exist on the test environments in case you're testing the redirect!
When using params with the redirect_to method be aware of the security implications. Check out my blog post and the "Response Splitting" post in the Rails weblog.
Using the helper method link_to with the :back option would be the best for displaying the "Continue shopping" link.
link_to('Continue shopping', :back)
I am trying to use the redirect_to :back from an action, but I am using restful_authentication, and the action requires the user to be logged in (I have a before_filter :login_required in the controller), so when I am already logged in the :back works fine, but when it redirects first to the login page, when it gets to the redirection in my action, it tries to go back to /session/new...
Is there any easy way to fix this?
You are the BEST!!!
@Ryan "But don't you need to use an after_filter instead of a before_filter so it resets at the end of the request instead of the beginning?"
Yes, must use a after_filter, that's exactly how I implement mine. Plus it keeps it from muddying up the controller.
This episode has been updated to Rails 5 as a blog post Going Back in Rails 5
First sign in through GitHub to post a comment.