当用户无法使用Devise登录时重定向问题

在我的应用程序中,我使用Devise对用户进行身份validation,我注意到如果登录失败,您可以更改重定向到的页面。 在维基上,我找到了以下示例:

class CustomFailure  '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)并放在帮助文件夹中(不知道放在哪里)。 这是我创建的以下课程:

 class CustomFailure  '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位于: 如何:当用户无法进行身份validation时,重定向到特定页面

尝试将custom_failure.rb放入lib文件夹,因为它不是帮助器。 然后确保加载文件。 可能你会尝试自动加载lib所有文件。

编辑 :要实现自动加载的库,您可以将以下内容插入您的application.rb

 config.autoload_paths += %W(#{config.root}/lib) 

EDIT2 :您在尝试redirect_to时忘了调用redirect_to。 目前respond只返回root_url 。 试试这个:

 def respond if http_auth? http_auth else redirect_to root_url end end 

fyi:设计人员在重定向之前也执行此操作:

 store_location! flash[:alert] = i18n_message unless flash[:notice]