无法覆盖Devise中的after_omniauth_failure_path_for

时间:2012-04-19 11:49:00

标签: facebook ruby-on-rails-3 devise omniauth

我有我的班级用户:: OmniauthCallbacksController<设计:: OmniauthCallbacksController 我正在覆盖after_omniauth_failure_path_for方法:

protected
  def after_omniauth_failure_path_for resource
    '/report_failure'
  end

但超级的是被召唤的那个。

我怀疑这是因为passthru解决方法:

  devise_for :users do
    get '/users/auth/:provider'  => 'users/omniauth_callbacks#passthru'
  end

有人知道如何克服这个问题吗? 我正在使用Devise 2.0.4

这是日志文件报告:

Started GET "/users/auth/facebook/callback?error_reason=user_denied&error=access_denied&error_description=The+user+denied+your+request." for 77.124.184.115 at 2012-04-19 11:34:46 +0000
Processing by Devise::OmniauthCallbacksController#failure as HTML
  Parameters: {"error_reason"=>"user_denied", "error"=>"access_denied", "error_description"=>"The user denied your request."}
Redirected to http://myapp.com/users/sign_in
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)

编辑:如果我删除了passthru重定向,那么它可以工作:

#       def  devise_for :users do
#        get '/users/auth/:provider'  => 'users/omniauth_callbacks#passthru'
#      end

谢谢

2 个答案:

答案 0 :(得分:0)

您忘记添加:controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } devise_for路由配置的参数,没有它omniauth不知道在失败后重定向到哪个控制器。

顺便说一下,passthru不是设计的一部分。你可能自己添加了它?

答案 1 :(得分:0)

我在omniauth_callbacks中正确设置了routes.rb控制器,但是仍然无法覆盖failureafter_omniauth_failure_path_for方法。问题在于,在出现问题的用户模型之前,我还设计了一个具有空路径的用户模型:

devise_for :customer_users, path: ''
# [...]
devise_for :users, module: 'users' # using a module instead of controllers

这导致始终匹配this devise omniauth方法中的customer_user映射。因此,故障由默认的Devise::OmniauthCallbacksController处理:

Processing by Devise::OmniauthCallbacksController#failure as HTML

可以通过为customer_users设置非空路径来解决此问题:

devise_for :customer_users, path: 'customer'
# or the default
devise_for :customer_users

我希望这有助于防止有人犯同样的错误。