2

好吧,我正在尝试编写一个使用 facebook 登录的系统。当我单击 facebook 链接时,它会转到 facebook 页面进行身份验证,并在回调中显示此错误:无法自动加载常量 Users::OmniauthCallbacksController,预期路径/app/controllers/users/omniauth_callbacks_controller.rb 来定义它。我正在使用 devise gem、omniauth gem、ruby 2 和 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

我的用户.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

routes.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>'
4

2 回答 2

0

如果您的回调控制器位于一个名为user_controller.rb(正如您的帖子所暗示的那样)的文件中,那么这是不正确的。

要遵循 Rails 的命名约定,它需要 in app/controllers/users/omniauth_callbacks_controller.rb(正如错误告诉你的那样)。

您也有命名空间的问题 - 您的回调控制器的路由是指users(复数),而您的控制器命名空间是User(单数)。

于 2013-10-25T06:45:12.990 回答
0

尝试将以下内容添加到用户模型: :omniauthable, :omniauth_providers => [:facebook]

于 2013-10-25T06:42:13.087 回答