rails 应用程序 ruby 中使用 gem omniauth-facebook 的 omniauth_callbacks_controller 的未初始化常量用户

uninitialized constant Users for omniauth_callbacks_controller with gem omniauth-facebook in ruby on rails app

我在使用 rails 设计和 omniauth-facebook 时遇到问题。我安装了 gem omniauth-facebookgem devise.

我已经在 config/application.yml 中设置了我的 fb 密码和密钥。我在 # config/initializers/devise.rb 中配置了设计:

Devise.setup do |config|
  config.omniauth :facebook, ENV["FB_ID"], ENV["FB_SECRET"], scope: 'email', info_fields: 'email, first_name,last_name', image_size: 'large'
end

我修改了我的路线如下:

config/routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
end

我更改了我的用户模型:

# app/models/user.rb

class User < ActiveRecord::Base
  devise :omniauthable, omniauth_providers: [:facebook]
end

我添加了以下 rails 迁移:

$ rails g migration AddOmniauthToUsers \
    provider uid picture first_name last_name token token_expiry:datetime
$ rake db:migrate

在我的用户模型中:

# app/models/user.rb
class User < ActiveRecord::Base
  def self.find_for_facebook_oauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]  # Fake password for validation
      user.first_name = auth.info.first_name
      user.last_name = auth.info.last_name
      user.picture = auth.info.image
      user.token = auth.credentials.token
      user.token_expiry = Time.at(auth.credentials.expires_at)
    end
  end
end

我创建了一个新控制器来处理 Omniauth 回调请求

# app/controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    user = User.find_for_facebook_oauth(request.env['omniauth.auth'])

    if user.persisted?
      sign_in_and_redirect user, event: :authentication
      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

我的 facebook connect 在我的本地应用程序上运行,但它使应用程序从一开始就在 heroku 上崩溃,并显示以下错误消息:

我已经在 heroku 和 heroku 运行 rake db:migrate.

上正确设置了我的环境变量

此错误从何而来,我该如何解决?

/app/app/controllers/Users/omniauth_callbacks_controller.rb:1:in `<top (required)>': uninitialized constant Users (NameError)
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:471:in `each'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:471:in `block in eager_load!'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:469:in `each'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:469:in `eager_load!'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/engine.rb:346:in `eager_load!'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/application/finisher.rb:56:in `each'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/application/finisher.rb:56:in `block in <module:Finisher>'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `call'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:347:in `each_strongly_connected_component'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:226:in `tsort_each'
    from /app/vendor/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:205:in `tsort_each'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
    from /app/config/environment.rb:5:in `<top (required)>'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/application.rb:328:in `require_environment!'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:142:in `require_application_and_environment!'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:67:in `console'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /app/vendor/bundle/ruby/2.3.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
    from /app/bin/rails:9:in `require'
    from /app/bin/rails:9:in `<main>'

1st possible solution:

The name error uninitialized constant class will occur if your rails console is not loaded with configuration of the class file containing method being called. It means that the class was just not loaded with the application.

Do you see this config.autoload_paths += %W(#{config.root}/lib) in your application.rb file? if not can you put it there and try again? let me know how it goes.

第二种可能的解决方案:

try to switch back to:

 devise_for :users, controllers: { omniauth_callbacks: 'omniauth_callbacks' 

当然还有移动 chnaged

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

到app/controllers/omniauth_callbacks_controller.rb

希望这对您有所帮助!让我知道