如何在 Ruby On Rails 中重定向到上一页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2139996/
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
How to redirect to previous page in Ruby On Rails?
提问by easement
I have a page that lists all of the projects that has sortable headers and pagination.
我有一个页面列出了所有具有可排序标题和分页的项目。
path:
/projects?order=asc&page=3&sort=code
I choose to edit one of the projects
我选择编辑其中一个项目
path:
projects/436/edit
When I click save on that page, it calls the projects controller / update method. After I update the code I want to redirect to the path that I was on before I clicked edit a specific project. In other words, I want to be on the same page with the same sorting.
当我单击该页面上的保存时,它会调用项目控制器/更新方法。更新代码后,我想重定向到单击编辑特定项目之前所在的路径。换句话说,我想以相同的排序在同一页面上。
I saw link_to(:back) and thought that :back may work in redirect_to(:back), but that's a no go.
我看到了 link_to(:back) 并认为 :back 可以在 redirect_to(:back) 中工作,但这是不行的。
puts YAML::dump(:back)
yields the following:
:back
Any ideas on How I could get this to work. It seems like a problem that would be easily solved, but I'm new to RoR.
关于如何让它发挥作用的任何想法。这似乎是一个很容易解决的问题,但我是 RoR 的新手。
回答by Jaime Bellmyer
In your edit action, store the requesting url in the session hash, which is available across multiple requests:
在您的编辑操作中,将请求 url 存储在会话哈希中,该哈希可跨多个请求使用:
session[:return_to] ||= request.referer
Then redirect to it in your update action, after a successful save:
然后在成功保存后在更新操作中重定向到它:
redirect_to session.delete(:return_to)
回答by Pascal
Why does redirect_to(:back)not work for you, why is it a no go?
为什么不redirect_to(:back)适合你,为什么不行?
redirect_to(:back)works like a charm for me. It's just a short cut for
redirect_to(request.env['HTTP_REFERER'])
redirect_to(:back)对我来说就像一种魅力。这只是一个捷径
redirect_to(request.env['HTTP_REFERER'])
http://apidock.com/rails/ActionController/Base/redirect_to(pre Rails 3) or http://apidock.com/rails/ActionController/Redirecting/redirect_to(Rails 3)
http://apidock.com/rails/ActionController/Base/redirect_to(Rails3 之前)或http://apidock.com/rails/ActionController/Redirecting/redirect_to(Rails3)
Please note that redirect_to(:back)is being deprecated in Rails 5. You can use
请注意,redirect_to(:back)在 Rails 5 中已弃用。您可以使用
redirect_back(fallback_location: 'something')instead (see http://blog.bigbinary.com/2016/02/29/rails-5-improves-redirect_to_back-with-redirect-back.html)
redirect_back(fallback_location: 'something')相反(参见http://blog.bigbinary.com/2016/02/29/rails-5-improves-redirect_to_back-with-redirect-back.html)
回答by Tony
I like Jaime's method with one exception, it worked better for me to re-store the referer every time:
我喜欢 Jaime 的方法,但有一个例外,每次重新存储引用对我来说效果更好:
def edit
session[:return_to] = request.referer
...
The reason is that if you edit multiple objects, you will always be redirected back to the first URL you stored in the session with Jaime's method. For example, let's say I have objects Apple and Orange. I edit Apple and session[:return_to]gets set to the referer of that action. When I go to edit Oranges using the same code, session[:return_to]will not get set because it is already defined. So when I update the Orange, I will get sent to the referer of the previous Apple#edit action.
原因是如果您编辑多个对象,您将始终被重定向回您使用 Jaime 方法存储在会话中的第一个 URL。例如,假设我有对象 Apple 和 Orange。我编辑 Apple 并session[:return_to]设置为该操作的引用者。当我使用相同的代码编辑 Oranges 时,session[:return_to]不会设置,因为它已经定义。因此,当我更新 Orange 时,我将被发送到之前 Apple#edit 操作的引用者。
回答by MBO
This is how we do it in our application
这就是我们在应用程序中的做法
def store_location
session[:return_to] = request.fullpath if request.get? and controller_name != "user_sessions" and controller_name != "sessions"
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
end
This way you only store last GET request in :return_tosession param, so all forms, even when multiple time POSTed would work with :return_to.
通过这种方式,您只将最后一个 GET 请求存储在:return_to会话参数中,因此所有表单,即使多次 POSTed 也可以使用:return_to.
回答by Steve Tipton
request.refereris set by Rack and is set as follows:
request.referer由 Rack 设置,设置如下:
def referer
@env['HTTP_REFERER'] || '/'
end
Just do a redirect_to request.refererand it will always redirect to the true referring page, or the root_path ('/'). This is essential when passing tests that fail in cases of direct-nav to a particular page in which the controller throws a redirect_to :back
只需执行一个redirect_to request.referer,它就会始终重定向到真正的引用页面,或 root_path ('/')。当将直接导航失败的测试传递到控制器抛出 redirect_to 的特定页面时,这是必不可少的:back
回答by pSkarl
In rails 5, as per the instructions in Rails Guides, you can use:
在 Rails 5 中,按照 Rails 指南中的说明,您可以使用:
redirect_back(fallback_location: root_path)
The 'back' location is pulled from the HTTP_REFERER header which is not guaranteed to be set by the browser. Thats why you should provide a 'fallback_location'.
'back' 位置是从 HTTP_REFERER 标头中提取的,该标头不保证由浏览器设置。这就是为什么你应该提供一个“fallback_location”。
回答by aschyiel
For those who are interested, here is my implementation extending MBO's original answer (written against rails 4.2.4, ruby 2.1.5).
对于那些感兴趣的人,这是我的实现扩展了 MBO 的原始答案(针对 rails 4.2.4、ruby 2.1.5 编写)。
class ApplicationController < ActionController::Base
after_filter :set_return_to_location
REDIRECT_CONTROLLER_BLACKLIST = %w(
sessions
user_sessions
...
etc.
)
...
def set_return_to_location
return unless request.get?
return unless request.format.html?
return unless %w(show index edit).include?(params[:action])
return if REDIRECT_CONTROLLER_BLACKLIST.include?(controller_name)
session[:return_to] = request.fullpath
end
def redirect_back_or_default(default_path = root_path)
redirect_to(
session[:return_to].present? && session[:return_to] != request.fullpath ?
session[:return_to] : default_path
)
end
end

