【问题标题】:Spring and middleware conflict?Spring和中间件冲突?
【发布时间】:2016-12-04 12:30:53
【问题描述】:

我正在尝试向 Google 添加 oauth2,关注 these instructions

我在启动服务器时收到以下错误消息:

Exiting
You've tried to invoke Spring when it's already loaded (i.e. the Spring constant is defined).

This is probably because you generated binstubs with Spring 1.0, and you now have a Spring version > 1.0 on your system. To solve this, upgrade your bundle to the latest Spring version and then run `bundle exec spring binstub --all` to regenerate your binstubs. This is a one-time step necessary to upgrade from 1.0 to 1.1.

Here's the backtrace:

/Users/omonia/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
/Users/omonia/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `block in load'
/Users/omonia/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
/Users/omonia/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
/Users/omonia/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `<top (required)>'
/Users/omonia/.rbenv/versions/2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in `require'
/Users/omonia/.rbenv/versions/2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:133:in `rescue in require'
/Users/omonia/.rbenv/versions/2.3.1/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:40:in `require'
/Users/omonia/Dropbox/MyApp/bin/spring:13:in `<top (required)>'

安装的相关gem:

gem 'google-api-client', '0.9'
gem 'omniauth'
gem 'omniauth-google_oauth2'

以下代码添加到config/initializers/omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, 'my Google client id', 'my Google client secret', {client_options: {ssl: {ca_file: Rails.root.join("cacert.pem").to_s}}}
end

这里发生了什么?遵循错误说明不会改变任何东西(Spring 是最新版本 1.7.2 并且运行 bundle exec spring binstub --all 只会返回“spring 已经存在”)。

【问题讨论】:

  • 你升级了Spring 吗?
  • 你能先运行bin/spring binstub --remove --all,然后运行bundle exec spring binstub --all吗?确保在所有这些命令之前升级 spring
  • 我现在按照您的步骤操作,但仍然出现相同的错误消息。我应该补充一点,这是一个新应用程序,所以几乎没有其他令人不安的代码。要添加的第二件事:如果我删除了omniauth-initializer 中的代码 - 我不会再收到任何错误。
  • 如果你去掉整个弹簧呢?我认为这应该有效。只需执行bin/spring binstub --remove --all 并从Gemfile 中删除gem 并再次运行bundle install。当然然后重启服务器。
  • 我照你说的做了。错误消失了,但出现了另一个关于 oauth 的错误。在搜索该错误I learned 之后,omniauth-google-oauth2 gem 拼写错误(用连字符代替下划线)。修复第二个错误后,我尝试重新添加 Spring,现在原始错误也消失了。一千次感谢@ArupRakshit 为我指明了正确的方向!

标签: ruby-on-rails oauth-2.0 ruby-on-rails-5 google-oauth rails-spring


【解决方案1】:

将我的comment 作为答案,因为 OP 确认它确实对他有帮助。按照以下步骤安装新的 Spring 并解决问题:

  • 运行这个bin/spring binstub --remove --all
  • 从 Gemfile 中删除 gem 并运行 bundle install
  • 现在在 Gemfile 中添加gem "spring", group: :development,在doc 之后运行bundle installbundle exec spring binstub --all。现在一切都会好起来的。

【讨论】:

  • 我尝试了这些步骤,但直到我删除了 tmp/cachevendor/bundle 才起作用
【解决方案2】:

检查 config/initializers/omniauth.rb 文件并确保您有以下内容:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google, 'Client_Id', 'Client_Secret'
end

确保您确实拥有 google 提供程序(不是 github 或 facebook)

【讨论】:

    【解决方案3】:

    这对我有用 -

    我在控制台上运行了这个 - rake rails:update:bin

    然后我跑了bundle exec spring binstub --all

    我不保证这对所有人都有效。

    谢谢:)

    【讨论】:

      【解决方案4】:

      代码升级到新的rails版本后我遇到了类似的问题,下面的评论帮助我解决了这个问题:

      https://github.com/rails/spring/issues/610#issuecomment-578188439

      基本上,在bin/rails stub 中禁用spring loader,然后运行rails s,直到找到并修复所有代码问题,最后启用spring back。

      【讨论】:

        【解决方案5】:

        首先,我不是 Ruby On Rails 专家,如果我在以下步骤中做错了什么,请纠正我。

        对我有用的步骤如下,

        从 MacOS 终端运行以下..

        bin/spring binstub --remove --all

        ** 删除 Vender 下的缓存文件夹(使用 Finder)

        捆绑安装。

        rails 资产:预编译

        【讨论】:

          【解决方案6】:

          在将我的 ruby​​ 版本从 2.6.1 升级到 2.7.1 后,我遇到了类似的问题 和轨道 5 到轨道 6。

          我阅读了讨论Here 并在 bin/rails

          中禁用我的 loading spring code
          #!/usr/bin/env ruby
          begin
            load File.expand_path('../spring', __FILE__)
          rescue LoadError => e
            raise unless e.message.include?('spring')
          end
          APP_PATH = File.expand_path('../config/application', __dir__)
          require_relative '../config/boot'
          require 'rails/commands'
          

          到:

          #!/usr/bin/env ruby
          APP_PATH = File.expand_path('../config/application', __dir__)
          require_relative '../config/boot'
          require 'rails/commands'
          

          我收到了错误

          /home/humayun/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require': 无法加载此类文件 -- thwait (LoadError)

          我通过将以下 gem 添加到我的 gem 文件来解决此问题。

          gem 'cmath'
          gem 'scanf'
          gem 'shell'
          gem 'sync'
          gem 'thwait'
          gem 'e2mmap'
          

          此后,我的 rails 工作正常,我将 bin/rails 移至其原始代码。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-01-14
            • 2018-10-19
            • 2016-03-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-23
            • 1970-01-01
            相关资源
            最近更新 更多