ActiveSupport::Dependencies:Module 的未定义方法 `mechanism=' (NoMethodError)

在 rails 7.0.0 中生成控制器时遇到问题

当我运行命令时

rails g controller Home index

它给了我以下错误。

/home/dev/.rvm/gems/ruby-3.0.2/gems/spring-2.1.1/lib/spring/application.rb:103:in `block in preload': undefined method `mechanism=' for ActiveSupport::Dependencies:Module (NoMethodError)
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/railties-7.0.1/lib/rails/initializable.rb:32:in `instance_exec'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/railties-7.0.1/lib/rails/initializable.rb:32:in `run'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/railties-7.0.1/lib/rails/initializable.rb:61:in `block in run_initializers'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:228:in `block in tsort_each'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:431:in `each_strongly_connected_component_from'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:349:in `block in each_strongly_connected_component'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `each'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `call'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:347:in `each_strongly_connected_component'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:226:in `tsort_each'
    from /home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/tsort.rb:205:in `tsort_each'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/railties-7.0.1/lib/rails/initializable.rb:60:in `run_initializers'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/railties-7.0.1/lib/rails/application.rb:372:in `initialize!'
    from /home/dev/project/spousal/config/environment.rb:5:in `<main>'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/bootsnap-1.10.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/zeitwerk-2.5.3/lib/zeitwerk/kernel.rb:35:in `require'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/spring-2.1.1/lib/spring/application.rb:106:in `preload'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/spring-2.1.1/lib/spring/application.rb:157:in `serve'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
    from /home/dev/.rvm/gems/ruby-3.0.2/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
    from <internal:/home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from <internal:/home/dev/.rvm/rubies/ruby-3.0.2/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
    from -e:1:in `<main>

我用谷歌搜索但没有找到解决方案。那么这个错误的原因是什么?

stack overflow undefined method `mechanism=' for ActiveSupport::Dependencies:Module (NoMethodError)
原文答案

答案:

作者头像

我最近将一个升级到 Rails 7.0.1 并遇到了同样的问题。我在尝试运行 rails console 时看到了同样的错误。

当我从 Gemfile 中删除 spring-watcher-listen (因为它阻止 spring 更新过去的 2.x )然后在 Gemfile 中将 spring 撞到 3.0.0 时,我通过了它。然后我运行 bundle 并且 rails console 再次工作。

这似乎是我们在升级 Rails 时必须克服的障碍,如 this article under "6. Notable Changes" 中所述。

作者头像

将 rails 6 应用程序更新到 rails 7 后遇到此问题。我尝试执行 rails g model User 命令。

我通过直接使用 rails 7 重新创建项目来解决它。

如果您有升级到 rails 7 的现有项目,则无法应用此解决方案。最好参考官方转换 rails guide

作者头像

同样在这里,在 rails g migration .. 上。更新到 rails-7.0.4 后。尽管我在我的 gemfile gem "spring" 中有并运行了 bundle update ,但 spring 在 2.x 上运行。在 Gemfile 中将其更改为 gem 'spring', '~> 4.1' 运行包并且它可以工作。