【问题标题】:undefined method `mechanism=' for ActiveSupport::Dependencies:Module (NoMethodError)ActiveSupport::Dependencies:Module 的未定义方法 `mechanism=' (NoMethodError)
【发布时间】:2022-03-13 20:55:32
【问题描述】:

在 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>

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

【问题讨论】:

    标签: dependencies activesupport


    【解决方案1】:

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

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

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

    【讨论】:

    • 也为我工作,谢谢!
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-04-13
      • 2013-04-24
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 2019-06-12
      • 2013-11-14
      相关资源
      最近更新 更多