Rails - how can I grab the url that was directed to the current activity in my application?

I need to see which page the request appeared on, in my controller, so that I can redirect back to it. For example, if I am on a page with a specific product (say /products/1

) and it has a link to its vendor ( /vendors/12

), I want to be able to detect inside vendors_controller that I came to that page from /products/1

. Is there an easy way in Rails to achieve this so that I can access it via params or session? thanks.

+2


a source to share


2 answers


redirect_to :back

can be used in the controller to redirect to the page where the request was made. It uses the value of the title HTTP_REFERER

to determine where to return.



+4


a source


You can also use

redirect_to request.referrer

      

as described here http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html



To be honest, I think this is what the previous thing does. Also see this elsewhere on S / O

Execute redirect_to correctly: return to Ruby on Rails when referrer is not available

0


a source







All Articles