2013-10-25 28 views
1

好吧,我想編寫一個使用Facebook登錄的系統。當我點擊Facebook鏈接時,它進入Facebook頁面進行身份驗證,並在回調中顯示此錯誤:無法自動加載常量用戶:: OmniauthCallbacksController,預計路徑 /app/controllers/users/omniauth_callbacks_controller.rb來定義它。 我使用設計的寶石,寶石omniauth,紅寶石2和鋼軌4LoadError在用戶:: OmniauthCallbacksController#facebook - Rails 4

user_controller.rb

class User::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def facebook 
    @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 

我user.rb

class User < ActiveRecord::Base 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable 

    def self.find_for_facebook_oauth(auth, signed_in_resource = nil) 
    user = User.where(:provider => auth.provider, :uid => auth.uid).first 
    if user 
     return user 
    else 
     registered_user = User.where(:email => auth.info.email).first 
     if registered_user 
     return registered_user 
     else 
     user = User.create(name:auth.extra.raw_info.name, 
          provider:auth.provider, 
          uid:auth.uid, 
          email:auth.info.email, 
          password:Devise.friendly_token[0,20], 
     ) 
     end 
    end 
    end 
end 

路線。 rb我有這條線:

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

的完整曲線是這樣的:

activesupport (4.0.0) lib/active_support/dependencies.rb:463:in `load_missing_constant' 
activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing' 
activesupport (4.0.0) lib/active_support/inflector/methods.rb:228:in `const_get' 
activesupport (4.0.0) lib/active_support/inflector/methods.rb:228:in `block in constantize' 
activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in `each' 
activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in `inject' 
activesupport (4.0.0) lib/active_support/inflector/methods.rb:224:in `constantize' 
activesupport (4.0.0) lib/active_support/dependencies.rb:534:in `get' 
activesupport (4.0.0) lib/active_support/dependencies.rb:565:in `constantize' 
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:76:in `controller_reference' 
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:66:in `controller' 
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:44:in `call' 
actionpack (4.0.0) lib/action_dispatch/routing/mapper.rb:44:in `call' 
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call' 
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each' 
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call' 
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call' 
omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' 
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' 
omniauth (1.1.4) lib/omniauth/strategy.rb:184:in `call!' 
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' 
omniauth (1.1.4) lib/omniauth/strategy.rb:401:in `call_app!' 
omniauth (1.1.4) lib/omniauth/strategy.rb:363:in `callback_phase' 
omniauth-oauth2 (1.1.1) lib/omniauth/strategies/oauth2.rb:77:in `callback_phase' 
omniauth (1.1.4) lib/omniauth/strategy.rb:226:in `callback_call' 
omniauth (1.1.4) lib/omniauth/strategy.rb:182:in `call!' 
omniauth (1.1.4) lib/omniauth/strategy.rb:164:in `call' 
warden (1.2.3) lib/warden/manager.rb:35:in `block in call' 
warden (1.2.3) lib/warden/manager.rb:34:in `catch' 
warden (1.2.3) lib/warden/manager.rb:34:in `call' 
rack (1.5.2) lib/rack/etag.rb:23:in `call' 
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call' 
rack (1.5.2) lib/rack/head.rb:11:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call' 
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context' 
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call' 
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call' 
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call' 
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call' 
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__878755641__call__callbacks' 
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks' 
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call' 
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app' 
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call' 
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged' 
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged' 
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged' 
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call' 
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call' 
rack (1.5.2) lib/rack/runtime.rb:17:in `call' 
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call' 
rack (1.5.2) lib/rack/lock.rb:17:in `call' 
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call' 
railties (4.0.0) lib/rails/engine.rb:511:in `call' 
railties (4.0.0) lib/rails/application.rb:97:in `call' 
rack (1.5.2) lib/rack/lock.rb:17:in `call' 
rack (1.5.2) lib/rack/content_length.rb:14:in `call' 
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service' 
C:/Development/Ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service' 
C:/Development/Ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run' 
C:/Development/Ruby200/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread' 

所以這是我的問題,我希望有人能幫助我。

編輯:而不是我所暗示的,我的回調控制器被稱爲omniauth_callbacks_controller.rb。 1. sevenseacat建議我從用戶用戶改變了我的路線文件的參考,但問題還是發生了。 2. * uno_ordinary *建議我加入:omniauth_providers =>:在我的模型,但在那之後的Facebook我啓動服務器時收到一個新的錯誤:

=> Booting WEBrick 
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000 
=> Run `rails server -h` for more startup options 
=> Ctrl-C to shutdown server 
Exiting 
C:3:in `rescue in execute_if_updated': Rails::Application::RoutesReloader#execute_if_updated delegated to updater.execute_if_updated, but updater is nil: #<Rails::Application::RoutesReloader:0x4375db8 @paths=["C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config/routes.rb"], @route_sets=[#<ActionDispatch::Routing::RouteSet:0x449da98>]> (RuntimeError) 
     from C:131071:in `execute_if_updated' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in `instance_exec' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in `run' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:55:in `block in run_initializers' 
     from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:150:in `block in tsort_each' 
     from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component' 
     from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:219:in `each_strongly_connected_component_from' 
     from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:182:in `block in each_strongly_connected_component' 
     from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:180:in `each' 
     from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:180:in `each_strongly_connected_component' 
     from C:/Development/Ruby200/lib/ruby/2.0.0/tsort.rb:148:in `tsort_each' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/initializable.rb:54:in `run_initializers' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/application.rb:215:in `initialize!' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing' 
     from C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config/environment.rb:5:in `<top (required)>' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require' 
     from C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config.ru:3:in `block in <main>' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize' 
     from C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config.ru:in `new' 
     from C:/Users/Diogo de Lima/Documents/Development/Ruby on Rails/Rhodnius/config.ru:in `<main>' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:199:in `app' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/server.rb:48:in `app' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/server.rb:75:in `start' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:78:in `block in <top (required)>' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in `tap' 
     from C:/Development/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in `<top (required)>' 
     from bin/rails:4:in `require' 
     from bin/rails:4:in `<main>' 

回答

0

嘗試添加到用戶模型如下: :omniauthable, :omniauth_providers => [:facebook]

0

如果您的回調控制器位於名爲user_controller.rb的文件中(正如您的文章所暗示的那樣),這是不正確的。

要遵循Rails的命名約定,它需要在app/controllers/users/omniauth_callbacks_controller.rb(因爲錯誤告訴你)。

您還有一個命名空間問題 - 您的回調控制器的路線指的是users(複數),而您的控制器命名空間是User(單數)。

相關問題