Ruby on Rails:如何获取之前的 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4652084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby on Rails: How do you get the previous URL?
提问by NullVoxPopuli
How to get the previous URL in Rails? Not the URL that you got a request from, but the one before that?
如何在 Rails 中获取以前的 URL?不是您收到请求的 URL,而是之前的 URL?
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).
因为我收到了一个 AJAX 请求,我需要他们当前所在页面的 URL(或 AJAX 之前的 URL)。
回答by gayavat
Try to use HTTP_REFERER.
尝试使用HTTP_REFERER.
In Rails: request.referreror request.headers["HTTP_REFERER"]
在 Rails 中:request.referrer或request.headers["HTTP_REFERER"]
回答by panicoper
Use
用
<%= url_for(:back) %>
# if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# => http://www.example.com
回答by troelskn
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.
在 Web 应用程序中,没有以前的 url 之类的东西。http 协议是无状态的,所以每个请求都是相互独立的。
You could have the Javascript code, that sends a request back, send the current url with the request.
您可以拥有发回请求的 Javascript 代码,将当前 url 与请求一起发送。
回答by Matt
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
我之前在使用 ajax 向导样式应用程序时采用的一个简单解决方案是存储两个会话变量,其中包含上一个和当前请求(带有所有参数)。我只是将这两行添加到我的应用程序控制器
session[:previous_request_url] = session[:current_request_url]
session[:current_request_url] = request.url
回答by apneadiving
http://railscasts.com/episodes/246-ajax-history-state-> great for ajax
http://railscasts.com/episodes/246-ajax-history-state-> 非常适合 ajax
http://ethilien.net/archives/better-redirects-in-rails/-> You could put in session as many previous url as you want.
http://ethilien.net/archives/better-redirects-in-rails/-> 您可以根据需要放入尽可能多的先前网址。

