Ruby on Rails: How do you get the previous URL?

In a web application there is no such thing as a previous url. The http protocol is stateless, so each request is independent of each other.

You could have the Javascript code, that sends a request back, send the current url with the request.


Try to use HTTP_REFERER.

In Rails: request.referrer or request.headers["HTTP_REFERER"]


Use

<%= url_for(:back) %>
# if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# => http://www.example.com

here is more details.