在我的应用程序中,我使用 Devise 对用户进行身份验证,我注意到如果登录失败,您可以更改重定向到的页面。在维基上,我找到了以下示例:
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
按照这个例子,我创建了我自己的 CustomFailure 类(custom_failure.rb)并放置在 helper 文件夹中(不知道把它放在哪里)。这是我创建的以下类:
class CustomFailure < Devise::FailureApp
def redirect_url
new_user_session_url(:subdomain => 'secure')
end
# Redirect to root_url
def respond
if http_auth?
http_auth
else
root_url
end
end
end
我还在 config/initializers/devise.rb 文件中添加了以下内容(因为 wiki 声明应该完成):
config.warden do |manager|
manager.failure_app = CustomFailure
end
虽然我没有收到任何错误,但当我错误地登录时,它仍然重定向到/users/sign_in 页面(不是根页面)并且没有加载任何内容(即使源不为空,页面也完全是白色的)。我的 CustomFailure 类是否有问题,或者它位于错误的文件夹中?
我正在使用 Rails 3.0.1 和 Devise 1.1.rc0。
找到此代码的 wiki 位于:How To: Redirect to a specific page when the user can not be authenticated
最佳答案
试试把 custom_failure.rb
走进你lib
文件夹,因为它不是 helper 。然后确保文件已加载。您可能会尝试加载 lib
中的所有文件自动地。
编辑 :要实现自动加载的库,您需要将以下内容插入您的 application.rb
:
config.autoload_paths += %W(#{config.root}/lib)
编辑2 : 你忘了打电话
redirect_to
尝试重定向时。目前respond
只返回 root_url
.尝试这个:def respond
if http_auth?
http_auth
else
redirect_to root_url
end
end
仅供引用:设计人员在重定向之前也会执行此操作:
store_location!
flash[:alert] = i18n_message unless flash[:notice]
关于ruby-on-rails - 用户无法使用 Devise 登录时的重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4180386/