簡體   English   中英

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

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

我有一個頁面列出了所有具有可排序標題和分頁的項目。

path:
/projects?order=asc&page=3&sort=code

我選擇編輯其中一個項目

path:
projects/436/edit

當我單擊該頁面上的保存時,它會調用項目控制器/更新方法。 更新代碼后,我想重定向到單擊編輯特定項目之前所在的路徑。 換句話說,我想以相同的排序在同一頁面上。

我看到了 link_to(:back) 並認為 :back 可以在 redirect_to(:back) 中工作,但這是不行的。

puts YAML::dump(:back) 
yields the following:
:back 

關於如何讓它發揮作用的任何想法。 這似乎是一個很容易解決的問題,但我是 RoR 的新手。

在您的編輯操作中,將請求 url 存儲在會話哈希中,該哈希可跨多個請求使用:

session[:return_to] ||= request.referer

然后在成功保存后在更新操作中重定向到它:

redirect_to session.delete(:return_to)

為什么redirect_to(:back)對你不起作用,為什么不行?

redirect_to(:back)對我來說就像一個魅力。 這只是redirect_to(request.env['HTTP_REFERER'])的捷徑

http://apidock.com/rails/ActionController/Base/redirect_to(Rails 3 之前)或http://apidock.com/rails/ActionController/Redirecting/redirect_to(Rails 3)

請注意,在 Rails 5 中, redirect_to(:back)已被棄用。您可以使用

redirect_back(fallback_location: 'something')代替(參見http://blog.bigbinary.com/2016/02/29/rails-5-improves-redirect_to_back-with-redirect-back.html

我喜歡 Jaime 的方法,但有一個例外,每次重新存儲引用對我來說效果更好:

def edit
    session[:return_to] = request.referer
...

原因是如果您編輯多個對象,您將始終被重定向回您使用 Jaime 方法存儲在會話中的第一個 URL。 例如,假設我有對象 Apple 和 Orange。 我編輯 Apple 並將session[:return_to]設置為該操作的引用者。 當我使用相同的代碼去編輯 Oranges 時, session[:return_to]不會被設置,因為它已經被定義了。 因此,當我更新 Orange 時,我將被發送到之前 Apple#edit 操作的引用者。

這就是我們在應用程序中的做法

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

通過這種方式,您只將最后一個 GET 請求存儲在:return_to會話參數中,因此所有表單,即使多次 POSTed 也可以與:return_to

在 Rails 5 中,按照 Rails 指南中的說明,您可以使用:

redirect_back(fallback_location: root_path)

'back' 位置是從 HTTP_REFERER 標頭中提取的,該標頭不保證由瀏覽器設置。 這就是為什么你應該提供一個“fallback_location”。

request.referer由 Rack 設置,設置如下:

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

只需執行redirect_to request.referer ,它就會始終重定向到真正的引用頁面,或root_path ('/')。 當將直接導航失敗的測試傳遞到控制器拋出 redirect_to 的特定頁面時,這是必不可少的:back

對於那些感興趣的人,這是我的實現擴展了 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

我想知道這是否有效

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

並在提交表單中使用@return_here 作為隱藏值。

當然,重新加載會殺死它,因此只需根據需要返回默認回退。

link_to 'get me back', :back

符號:back是您的瑞士軍刀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM