简体   繁体   中英

Rails - how can I capture the URL that directed to the current action in my app?

I need to see what page a request came from in my controller so I could be able to redirect back to it. For example, if I'm on a page showing a specific product (say /products/1 ) and it has a link to its vendor ( /vendors/12 ), I want to be able to detect inside the vendors_controller that I came to that page from /products/1 . Is there a simple way in Rails to achieve this, so that I could access it via params or session? thanks.

redirect_to :back can be used within a controller to redirect back to the page that issued the request. It uses the value of the HTTP_REFERER header to determine where to go back to.

You can also use

redirect_to request.referrer

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

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

Correctly doing redirect_to :back in Ruby on Rails when referrer is not available

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM