簡體   English   中英

重定向到當前頁面軌道

[英]Redirect to current page rails

我在我的應用程序導航欄中有一個登錄表單,我想知道是否有一種簡單的方法可以在sign_in時將sign_in重定向到您當前所在的任何頁面。 謝謝!

只要給出了引用者,您就可以redirect_to(:back) 但是,我建議你看看這個答案 ,因為那里提出的解決方案對於失敗的登錄和OAuth重定向等內容都具有彈性。

在Rails 5中,您應該使用redirect_back並添加回退路徑:

redirect_back(fallback_location: root_path)

如果您已經編寫了代碼來為您的用戶創建會話,那么只需在sessions_controller中添加以下行:

redirect_to root_url, :notice => "Logged in!"

在這里,您可以輸入您希望用戶重定向到的任何路徑,而不是root_url 但請確保你在routes.rb中有它
如果你考慮不成功的sign_in,你可以使用render方法重新加載你的sign_in視圖

來自Devise人的這個解決方案應該可以解決問題 ......

ApplicationController編寫以下內容:

after_filter :store_location

def store_location
  # store last url - this is needed for post-login redirect to whatever the user last visited.
  return unless request.get? 
  if (request.path != "/users/sign_in" &&
      request.path != "/users/sign_up" &&
      request.path != "/users/password/new" &&
      request.path != "/users/password/edit" &&
      request.path != "/users/confirmation" &&
      request.path != "/users/sign_out" &&
      !request.xhr?) # don't store ajax calls
    session[:previous_url] = request.fullpath 
  end
end

def after_sign_in_path_for(resource)
  session[:previous_url] || root_path
end

暫無
暫無

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

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