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?
Hi Ryan,
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:
http://api.rubyonrails.com/classes/ActionController/AbstractRequest.html#M000808
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.
Good episode.
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:
http://github.com/toothygoose/historic_routes/tree/master
It's not totally weaponized yet, but I think the concept is sound.
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.
http://pastie.org/292092
@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.
please also mention the most obviouse choise:
link_to 'Continue', :back
Ryan,
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.
Thanks,
kevin
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)
Wow, spammers are copying others comments. That's a new one.
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)
This is better because it will link to the referrer if it is present or "javascript:history.back()" when there is no referrer.
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?
@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.
Now this code will help me I my latest project.Thanks For posting it..
will this automatically create a button for my visitors to go to the previous page? Rather than going to the top and press the back button?
I tried the code and it worked perfectly, thanks alot
Yes!! I get frustrated by pressing back again and again.
I was searching for information on Railscast, and google directed me to this page.
I didn't get how to minimize the use of back button either we have to download or upload the code.
I was searching for information on Railscast, and google directed me to this page.
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.
It is very nice coding. This coding will very beneficial for our sites because by this coding user can visit one page to another page.
Its really very helpful code that you shared thanx a lot for this.
Thanks for the post. Idea behind your post is interesting.i would like to read more from you.
Nice informative post thanks for sharing with us.
I used your code in my project. Your code is fully operational.
I think episode has really reduced the overhead of pressing back button to show the previous page.
Thanks for posting. Idea behind your post is interesting.i would like to read more from you.
I have bookmarked you page, hopefully you add some more codes for us to use.
Redirecting using your code is very easy. If you some more details about this then please share it with me.
Thank you for this useful information.I will share it with my friends.I have done it bookmark.
You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!
I found your blog from google and read a few of your other posts.They are incredible. Please keep it up... have a nice day.
Hi people,
Well I am new here and really enjoyed reading the article.
Thanks for sharing this great piece of information.Do keep us update with some more great articles.
I found your blog from google and read a few of your other posts. They are incredible. Please keep it up… have a nice day.
this information is very nice and activities post. I hope it will get more benefit to me.







