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

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.


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.


Just came across this question and it related to something simple I was doing presently.

A simple solution I have employed before when using ajax wizard style apps is to store two session variables which contain the previous and current request (with all params). I just add these two lines to my application controller

session[:previous_request_url] = session[:current_request_url]
session[:current_request_url] = request.url