[英]How To: Redirect to a specific page when the user can not be authenticated (devise rails gem)
我只是想通了這一點,設計的指令不是最好的。 我在下面回答了我自己的問題
進入 lib
class CustomFailure < Devise::FailureApp
def redirect_url
new_user_session_url(:subdomain => 'secure')
end
# You need to override respond to eliminate recall
def respond
if http_auth?
http_auth
else
redirect
end
end
end
進入初始值設定項
config.warden do |manager|
manager.failure_app = CustomFailure
end
進入application.rb
config.autoload_paths << Rails.root.join('lib')
如果用戶未登錄,第一次關閉代碼將不會重定向您的路由。這是一個單獨的問題,可以在繼承到 registrations_controller 的自定義控制器中修復。 這將做的是在用戶登錄失敗時重定向。 IE 您輸入了錯誤的密碼 > 重定向到新頁面。
對於第一個代碼分支,在 lib 文件夾(頂級)中創建一個名為 custom_failure.rb 的應用程序並將該代碼粘貼到其中。
進入第二個代碼分支
/Users/admin/acltc_projects/roomkick/config/initializers/devise.rb 搜索
warden do
使用 cmd f 或 ctrl f 執行並刪除管理員執行和“結束”行上的 #。 將manager.failure_app = CustomFailure
粘貼到manager.failure_app = CustomFailure
正下方。 在 application.rb 的 config/locals 下添加config.autoload_paths << Rails.root.join('lib')
在你的模塊中。 最后回到 lib 中的應用程序並修改new_user_session_url(:subdomain => 'secure')
,您可以將其直接修改為您想要的任何路由。 重新啟動服務器並測試應用程序。 如果這有幫助,請點贊!
如果用戶無法進行身份驗證,您可以重定向到特定頁面。
對於本示例,我們希望使用具有特定子域的整個 URL。 默認情況下,設計使用路徑而不是整個 URL。 解決方法是使用從設計故障應用程序繼承的自定義故障應用程序。
class CustomFailure < Devise::FailureApp
def redirect_url
new_user_session_url(:subdomain => 'secure')
end
# You need to override respond to eliminate recall
def respond
if http_auth?
http_auth
else
redirect
end
end
end
並在 config/initializers/devise.rb 中添加以下內容:
config.warden do |manager|
manager.failure_app = CustomFailure
end
希望它會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.