用户登录失败后设计重定向

我知道有一个关于它的维基页面 ,但由于我对Rails很新,我在理解该页面时遇到了很多困难。

我不得不为我的用户覆盖注册控制器。 当用户无法登录时,我希望将他重定向到我的自定义登录页面。 但应用程序将他发送到gem内的登录页面。

我怎么能做到这一点? 我应该把这个课程放在哪里,如何更改? 我有多个模型,每个模型都有不同的登录页面。 如何设置每个型号的范围?

class CustomFailure  'secure') end # You need to override respond to eliminate recall def respond if http_auth? http_auth else redirect end end 

结束

Devise是一个强大的gem,但是一些wiki页面并不认为可以有很多新的程序员

我直接在/lib有我的auth故障覆盖类。

这是一个简单的版本,展示了如何为用户处理不同的范围。

 class MyAuthFailure < Devise::FailureApp # add different cases here for diff scopes. def redirect_url if warden_options[:scope] == :user root_path elsif warden_options[:scope] == :admin admin_root_path end end # You need to override respond to eliminate recall def respond if http_auth? http_auth else redirect end end end 

你将这个类放在/lib/my_auth_failure.rb

这个解决方案效果很好,但如果你不做本文所建议的,你会遇到测试问题:

https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-when-the-user-can-not-be-authenticated

您的测试仍将使用默认值重定向。

例如:

 current_path.should == signin_path 

会失败,因为当前路径实际上是users/sign_in或其他东西。