[英]Rails devise: Redirect user after log in to the url he wanted to go
由於可用性的原因,我需要將用戶重定向到他們想要轉到的頁面,而不是上一個頁面。
現在,如果Im在頁面B中 ,然后想轉到需要登錄的頁面A ,則用戶輸入其憑據,然后重定向到頁面B而不是頁面A。
這已成為我應用程序中非常嚴重的可用性問題。
假設您正在查看產品,然后單擊“立即購買”,那么您需要輸入登錄詳細信息,登錄后立即將您重定向到該產品,而不是結帳表格。
這是我的代碼的樣子:
class ApplicationController < ActionController::Base
after_filter :store_location
def store_location
# store last url as long as it isn't a /users path
session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/
end
def after_sign_in_path_for(resource)
session[:previous_url] || root_path
end
end
您正在尋找的是request.referer
。 當用戶去立即購買並重定向到登錄屏幕時,在這種情況下,立即購買 url是引薦來源。
只需將以下代碼復制到您的app / controllers / application_controller.rb文件中
class ApplicationController < ActionController::Base
protected
protect_from_forgery with: :exception
def after_sign_in_path_for(resource)
dashboard_index_path
end
end
希望對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.