Rails 6 omniauth with facebook 在本地有效但在 heroku 上无效

Rails 6 omniauth with facebook works locally but not on heroku

我是 rails 的新手,我正在尝试使用 amniauth 允许用户使用 facebook 登录。它在本地运行良好,但是当我部署到 heroku 时它崩溃了。在我添加 omniauth 之前,该应用程序正在运行。我将 facebook 应用程序中的域和回调更改为 heroku 应用程序地址。

这是我的 heroku 错误日志:

2020-12-16T21:48:39.769798+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require'
2020-12-16T21:48:39.769799+00:00 app[web.1]: from bin/rails:4:in `<main>'
2020-12-16T21:48:39.852489+00:00 heroku[web.1]: Process exited with status 1
2020-12-16T21:48:39.896554+00:00 heroku[web.1]: State changed from starting to crashed
2020-12-16T21:48:39.900314+00:00 heroku[web.1]: State changed from crashed to starting
2020-12-16T21:48:51.839873+00:00 heroku[web.1]: Starting process with command `bin/rails server -p ${PORT:-5000} -e production`
2020-12-16T21:49:03.244740+00:00 app[web.1]: => Booting Puma
2020-12-16T21:49:03.244785+00:00 app[web.1]: => Rails 6.0.3.4 application starting in production
2020-12-16T21:49:03.244785+00:00 app[web.1]: => Run `rails server --help` for more startup options
2020-12-16T21:49:05.218510+00:00 app[web.1]: Exiting
2020-12-16T21:49:05.224109+00:00 app[web.1]: /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader/callbacks.rb:18:in `on_file_autoloaded': expected file /app/app/controllers/concerns/registration_controller.rb to define constant RegistrationController, but didn't (Zeitwerk::NameError)
2020-12-16T21:49:05.224142+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:27:in `block in require'
2020-12-16T21:49:05.224195+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:26:in `tap'
2020-12-16T21:49:05.224219+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:26:in `require'
2020-12-16T21:49:05.224240+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:409:in `const_get'
2020-12-16T21:49:05.224259+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:409:in `block (2 levels) in eager_load'
2020-12-16T21:49:05.224277+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:733:in `block in ls'
2020-12-16T21:49:05.224296+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:725:in `foreach'
2020-12-16T21:49:05.224335+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:725:in `ls'
2020-12-16T21:49:05.224366+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:404:in `block in eager_load'
2020-12-16T21:49:05.224384+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:393:in `synchronize'
2020-12-16T21:49:05.224401+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:393:in `eager_load'
2020-12-16T21:49:05.224417+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:508:in `each'
2020-12-16T21:49:05.224434+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/loader.rb:508:in `eager_load_all'
2020-12-16T21:49:05.224451+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/application/finisher.rb:122:in `block in <module:Finisher>'
2020-12-16T21:49:05.224475+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/initializable.rb:32:in `instance_exec'
2020-12-16T21:49:05.224517+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/initializable.rb:32:in `run'
2020-12-16T21:49:05.224553+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/initializable.rb:61:in `block in run_initializers'
2020-12-16T21:49:05.224572+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:228:in `block in tsort_each'
2020-12-16T21:49:05.224591+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
2020-12-16T21:49:05.224609+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:431:in `each_strongly_connected_component_from'
2020-12-16T21:49:05.224627+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:349:in `block in each_strongly_connected_component'
2020-12-16T21:49:05.224645+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:347:in `each'
2020-12-16T21:49:05.224662+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:347:in `call'
2020-12-16T21:49:05.224694+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:347:in `each_strongly_connected_component'
2020-12-16T21:49:05.224721+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:226:in `tsort_each'
2020-12-16T21:49:05.224738+00:00 app[web.1]: from /app/vendor/ruby-2.7.1/lib/ruby/2.7.0/tsort.rb:205:in `tsort_each'    
2020-12-16T21:49:05.224755+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/initializable.rb:60:in `run_initializers'
2020-12-16T21:49:05.224772+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/application.rb:363:in `initialize!'
2020-12-16T21:49:05.224793+00:00 app[web.1]: from /app/config/environment.rb:5:in `<main>'
2020-12-16T21:49:05.224818+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
2020-12-16T21:49:05.224845+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
2020-12-16T21:49:05.224898+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
2020-12-16T21:49:05.224917+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
2020-12-16T21:49:05.224936+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
2020-12-16T21:49:05.224957+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/zeitwerk-2.4.2/lib/zeitwerk/kernel.rb:34:in `require'
2020-12-16T21:49:05.224975+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `block in require'
2020-12-16T21:49:05.225020+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
2020-12-16T21:49:05.225038+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require'
2020-12-16T21:49:05.225057+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:53:in `require_relative'
2020-12-16T21:49:05.225075+00:00 app[web.1]: from config.ru:3:in `block in <main>'
2020-12-16T21:49:05.225093+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/builder.rb:116:in `eval'
2020-12-16T21:49:05.225111+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/builder.rb:116:in `new_from_string'
2020-12-16T21:49:05.225128+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/builder.rb:105:in `load_file'
2020-12-16T21:49:05.225145+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/builder.rb:66:in `parse_file'
2020-12-16T21:49:05.225188+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/server.rb:349:in `build_app_and_options_from_config'
2020-12-16T21:49:05.225212+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/server.rb:249:in `app'
2020-12-16T21:49:05.225240+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/server.rb:422:in `wrapped_app'
2020-12-16T21:49:05.225266+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/server.rb:312:in `block in start'
2020-12-16T21:49:05.225290+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/server.rb:379:in `handle_profiling'
2020-12-16T21:49:05.225309+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/rack-2.2.3/lib/rack/server.rb:311:in `start'
2020-12-16T21:49:05.225356+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/commands/server/server_command.rb:39:in `start'
2020-12-16T21:49:05.225375+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/commands/server/server_command.rb:147:in `block in perform'
2020-12-16T21:49:05.225393+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/commands/server/server_command.rb:138:in `tap'
2020-12-16T21:49:05.225412+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/commands/server/server_command.rb:138:in `perform'
2020-12-16T21:49:05.225430+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/thor-1.0.1/lib/thor/command.rb:27:in `run'
2020-12-16T21:49:05.225447+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/thor-1.0.1/lib/thor/invocation.rb:127:in `invoke_command'
2020-12-16T21:49:05.225465+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/thor-1.0.1/lib/thor.rb:392:in `dispatch'
2020-12-16T21:49:05.225482+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/command/base.rb:69:in `perform'
2020-12-16T21:49:05.225524+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/command.rb:46:in `invoke'
2020-12-16T21:49:05.225541+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.3.4/lib/rails/commands.rb:18:in `<main>'
2020-12-16T21:49:05.225558+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
2020-12-16T21:49:05.225575+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
2020-12-16T21:49:05.225597+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
2020-12-16T21:49:05.225623+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
2020-12-16T21:49:05.225649+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/bootsnap-1.5.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
2020-12-16T21:49:05.225698+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `block in require'
2020-12-16T21:49:05.225717+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:291:in `load_dependency'
2020-12-16T21:49:05.225734+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.3.4/lib/active_support/dependencies.rb:324:in `require'
2020-12-16T21:49:05.225752+00:00 app[web.1]: from bin/rails:4:in `<main>'
2020-12-16T21:49:05.362989+00:00 heroku[web.1]: Process exited with status 1
2020-12-16T21:49:05.420179+00:00 heroku[web.1]: State changed from starting to crashed
2020-12-16T21:49:06.871012+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=12e387e2-c909-4188-83ee-4975cad25720 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:07.113474+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=73e98f1d-3714-43c2-8317-c11ce596731d fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:21.136469+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=ba5c6da6-7cac-46dd-b918-7d3bdffa23fd fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:21.321850+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=e863fdb8-f634-4338-8ddd-1ef2e1d18f71 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:25.312822+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=3d4f6e33-c9a6-4d7f-b915-ad49b64eb88c fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:25.529841+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=fa129c7d-798c-4fbf-9473-4b5ff1434677 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:54.197897+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=32b0746a-1912-4d5d-9e94-8ae9719d0dc4 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:54.449373+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=0bc3f7d2-2687-4e23-958d-8039c92da553 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:56.243616+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=9e647b29-3c81-4753-9493-f46f28397da9 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:56.419403+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=06ea91ef-d477-4c2d-8edf-67e744d70354 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:57.491031+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=87024f91-8536-4562-be85-472e35d992fa fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:57.665119+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=ee9eb4b7-aa02-4760-9b0e-9946f37855e3 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:57.839563+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=74d3f183-f8b7-4714-89b2-52cb6e5089c9 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:49:58.175431+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=d25bee3b-232f-4dac-95c9-e076ec4ded95 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:51:56.608279+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=34daf161-c82b-425b-9ed4-5ba2f0807b86 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:51:56.849575+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=418dd364-1027-4ec3-b46d-31b7125f793d fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:51:58.521848+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=the-mat-house.herokuapp.com request_id=68aaa403-3902-4ce2-bcae-096ee2921aeb fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https
2020-12-16T21:51:58.708040+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=the-mat-house.herokuapp.com request_id=54576357-5f49-478d-844a-4496bde41710 fwd="71.205.133.183" dyno= connect= service= status=503 bytes= protocol=https

这是我的 omniauth_controller.rb:

class OmniauthController < ApplicationController
  def facebook
    @user = User.create_from_provider_data(request.env['omniauth.auth'])
    if @user.persisted?
      flash[:notice] = "Signed in successfuly"
      sign_in_and_redirect @user
    else
      flash[:error] = 'There was a problem signing you in through facebook.  Please register or try signing in later.'
      redirect_to new_user_registration_url 
    end
  end

  def failure 
    flash[:error] = "There was a problem signing you in.  Please register or try signing in later."
    redirect_to root_path
  end
end

这是我的 user.rb 模型:

class User < ApplicationRecord
  has_many :user_clubs 
  has_many :clubs, through: :user_clubs
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable,
         :omniauthable, :omniauth_providers => [:facebook]

  def self.create_from_provider_data(provider_data)
    where(provider: provider_data.provider, uid: provider_data.uid).first_or_create do |user|
      user.email = provider_data.info.email 
      user.first_name = provider_data.info.first_name
      user.last_name = provider_data.info.last_name
      user.username = provider_data.info.email.split('@').first
      user.password = Devise.friendly_token[0,20]
    end
  end 
end

非常感谢任何帮助。

谢谢,

斯科特

不熟悉 Zeitwerk,但根据你的日志,它说

`on_file_autoloaded':期望文件 /app/app/controllers/concerns/registration_controller.rb 定义常量 RegistrationController,但没有 (Zeitwerk::NameError)

看起来与设计有关gem。检查这个可能有帮助的答案