5

我正在尝试按照本指南在我的应用程序中实现 facebook 身份验证

在 Facebook 上授权后,我得到以下信息:

Unknown action

The action 'facebook' could not be found for Devise::OmniauthCallbacksController

我已经在 app/controller/users/omniauth_callbacks_controller.rb 中实现了该方法:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    # You need to implement the method below in your model (e.g. app/models/user.rb)
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

    if @user.persisted?
      sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
    else
      session["devise.facebook_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

我的回调路线是:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

有什么我想念的吗?我该如何调试呢?

谢谢!

更新:添加了 rake 路由的输出(对不起长度):

          new_user_session GET    /users/sign_in(.:format)                   devise/sessions#new
              user_session POST   /users/sign_in(.:format)                   devise/sessions#create
      destroy_user_session DELETE /users/sign_out(.:format)                  devise/sessions#destroy
   user_omniauth_authorize        /users/auth/:provider(.:format)            devise/omniauth_callbacks#passthru {:provider=>/facebook/}
    user_omniauth_callback        /users/auth/:action/callback(.:format)     devise/omniauth_callbacks#(?-mix:facebook)
             user_password POST   /users/password(.:format)                  devise/passwords#create
         new_user_password GET    /users/password/new(.:format)              devise/passwords#new
        edit_user_password GET    /users/password/edit(.:format)             devise/passwords#edit
                           PUT    /users/password(.:format)                  devise/passwords#update
  cancel_user_registration GET    /users/cancel(.:format)                    devise/registrations#cancel
         user_registration POST   /users(.:format)                           devise/registrations#create
     new_user_registration GET    /users/sign_up(.:format)                   devise/registrations#new
    edit_user_registration GET    /users/edit(.:format)                      devise/registrations#edit
                           PUT    /users(.:format)                           devise/registrations#update
                           DELETE /users(.:format)                           devise/registrations#destroy
                      help        /help(.:format)                            static_pages#help
                     about        /about(.:format)                           static_pages#about
                   contact        /contact(.:format)                         static_pages#contact
                 users_new GET    /users/new(.:format)                       users#new
                    signup        /signup(.:format)                          users#new
           new_certificate        /new_certificate(.:format)                 users#newcertificate
                     users GET    /users(.:format)                           users#index
                           POST   /users(.:format)                           users#create
                  new_user GET    /users/new(.:format)                       users#new
                 edit_user GET    /users/:id/edit(.:format)                  users#edit
                      user GET    /users/:id(.:format)                       users#show
                           PUT    /users/:id(.:format)                       users#update
                           DELETE /users/:id(.:format)                       users#destroy
          new_user_session GET    /users/sign_in(.:format)                   devise/sessions#new
                           POST   /users/sign_in(.:format)                   devise/sessions#create
      destroy_user_session DELETE /users/sign_out(.:format)                  devise/sessions#destroy
   user_omniauth_authorize        /users/auth/:provider(.:format)            users/omniauth_callbacks#passthru {:provider=>/facebook/}
    user_omniauth_callback        /users/auth/:action/callback(.:format)     users/omniauth_callbacks#(?-mix:facebook)
                           POST   /users/password(.:format)                  devise/passwords#create
                           GET    /users/password/new(.:format)              devise/passwords#new
                           GET    /users/password/edit(.:format)             devise/passwords#edit
                           PUT    /users/password(.:format)                  devise/passwords#update
                           GET    /users/cancel(.:format)                    devise/registrations#cancel
                           POST   /users(.:format)                           devise/registrations#create
                           GET    /users/sign_up(.:format)                   devise/registrations#new
                           GET    /users/edit(.:format)                      devise/registrations#edit
                           PUT    /users(.:format)                           devise/registrations#update
                           DELETE /users(.:format)                           devise/registrations#destroy
                           GET    /users/auth/:provider(.:format)            users/omniauth_callbacks#passthru
                   courses GET    /courses(.:format)                         courses#index
                           POST   /courses(.:format)                         courses#create
                new_course GET    /courses/new(.:format)                     courses#new
               edit_course GET    /courses/:id/edit(.:format)                courses#edit
                    course GET    /courses/:id(.:format)                     courses#show
                           PUT    /courses/:id(.:format)                     courses#update
                           DELETE /courses/:id(.:format)                     courses#destroy
               enrollments GET    /enrollments(.:format)                     enrollments#index
                           POST   /enrollments(.:format)                     enrollments#create
            new_enrollment GET    /enrollments/new(.:format)                 enrollments#new
           edit_enrollment GET    /enrollments/:id/edit(.:format)            enrollments#edit
                enrollment GET    /enrollments/:id(.:format)                 enrollments#show
                           PUT    /enrollments/:id(.:format)                 enrollments#update
                           DELETE /enrollments/:id(.:format)                 enrollments#destroy
               submissions GET    /submissions(.:format)                     submissions#index
                           POST   /submissions(.:format)                     submissions#create
            new_submission GET    /submissions/new(.:format)                 submissions#new
           edit_submission GET    /submissions/:id/edit(.:format)            submissions#edit
                submission GET    /submissions/:id(.:format)                 submissions#show
                           PUT    /submissions/:id(.:format)                 submissions#update
                           DELETE /submissions/:id(.:format)                 submissions#destroy
    honour_code_signatures GET    /honour_code_signatures(.:format)          honour_code_signatures#index
                           POST   /honour_code_signatures(.:format)          honour_code_signatures#create
 new_honour_code_signature GET    /honour_code_signatures/new(.:format)      honour_code_signatures#new
edit_honour_code_signature GET    /honour_code_signatures/:id/edit(.:format) honour_code_signatures#edit
     honour_code_signature GET    /honour_code_signatures/:id(.:format)      honour_code_signatures#show
                           PUT    /honour_code_signatures/:id(.:format)      honour_code_signatures#update
                           DELETE /honour_code_signatures/:id(.:format)      honour_code_signatures#destroy
                      root        /                                          static_pages#home
4

1 回答 1

12

解决了我设置不正确的问题!我最初遵循设计教程并将其添加到我的路线中:

devise_for :users

然后我按照omniauth教程为回调添加了以下内容:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }

不知何故,这些肯定是相互冲突的,因为简单地删除第一个记录会将回调指向正确的方向(它一定是在寻找我将 Devise::OmniauthCallbacksController 子类化的 Devise 类)。

于 2013-02-03T00:52:34.330 回答