简体   繁体   English

如何在 Ruby On Rails 中重定向到上一页?

[英]How to redirect to previous page in Ruby On Rails?

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 的新手。

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)

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. redirect_to(:back)对我来说就像一个魅力。 It's just a short cut for redirect_to(request.env['HTTP_REFERER'])这只是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(Rails 3 之前)或http://apidock.com/rails/ActionController/Redirecting/redirect_to(Rails 3)

Please note that redirect_to(:back) is being deprecated in Rails 5. You can use请注意,在 Rails 5 中, redirect_to(:back)已被弃用。您可以使用

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

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.原因是如果您编辑多个对象,您将始终被重定向回您使用 Jaime 方法存储在会话中的第一个 URL。 For example, let's say I have objects Apple and Orange.例如,假设我有对象 Apple 和 Orange。 I edit Apple and session[:return_to] gets set to the referer of that action.我编辑 Apple 并将session[:return_to]设置为该操作的引用者。 When I go to edit Oranges using the same code, session[:return_to] will not get set because it is already defined.当我使用相同的代码去编辑 Oranges 时, session[:return_to]不会被设置,因为它已经被定义了。 So when I update the Orange, I will get sent to the referer of the previous Apple#edit action.因此,当我更新 Orange 时,我将被发送到之前 Apple#edit 操作的引用者。

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_to session param, so all forms, even when multiple time POSTed would work with :return_to .通过这种方式,您只将最后一个 GET 请求存储在:return_to会话参数中,因此所有表单,即使多次 POSTed 也可以与:return_to

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. 'back' 位置是从 HTTP_REFERER 标头中提取的,该标头不保证由浏览器设置。 Thats why you should provide a 'fallback_location'.这就是为什么你应该提供一个“fallback_location”。

request.referer is set by Rack and is set as follows: request.referer由 Rack 设置,设置如下:

def referer
  @env['HTTP_REFERER'] || '/'
end

Just do a redirect_to request.referer and it will always redirect to the true referring page, or the root_path ('/').只需执行redirect_to request.referer ,它就会始终重定向到真正的引用页面,或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 的特定页面时,这是必不可少的:back

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

I wonder if this will work我想知道这是否有效

 def edit if request.referer != request.original_url @return_here = request.referer end end

and use @return_here as a hidden value in the submit form.并在提交表单中使用@return_here 作为隐藏值。

of course reloading will kill this so just go back to a default fall back as needed.当然,重新加载会杀死它,因此只需根据需要返回默认回退。

link_to 'get me back', :back

符号:back是您的瑞士军刀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM