开发者

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

开发者 https://www.devze.com 2023-02-03 18:03 出处:网络
How to get the previous URL in Rails? Not the URL that you got a request from, but the one before that?

How to get the previous URL in Rails? Not the URL that you got a request from, but the one before that?

Because I'm getting an AJAX request, and I need the URL for the page they are currently on (or the URL before the AJAX)开发者_JAVA百科.


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


http://railscasts.com/episodes/246-ajax-history-state -> great for ajax

http://ethilien.net/archives/better-redirects-in-rails/ -> You could put in session as many previous url as you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消