views:

41

answers:

2

Hi -

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.

+2  A: 

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.

John Topley
Cool, this is pretty simple. thanks!
sa125
I didn't know you could do this - fantastic!
Stephen Orr
A: 

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

http://stackoverflow.com/questions/771656/correctly-doing-redirect-to-back-in-ruby-on-rails-when-referrer-is-not-available

Ghoti