生产环境下的Rails控制台:NameError: 未初始化的常量

34

我有一个Rails应用(Rails 5)。在开发中,一切正常,当我使用

rails console

输入指令,比如User.all,它会正常工作。

在生产环境中,我的应用程序运行完美,没有问题,也没有错误。但是当我使用rails console production并输入例如User.all时,出现了一个错误:

NameError: uninitialized constant User
    from (irb):2
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in `start'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:in `start'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `block in require'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/dependencies.rb:293:in `require'
    from /home/alexandre/tcheen/bin/rails:9:in `<top (required)>'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `load'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/commands/rails.rb:6:in `call'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/command_wrapper.rb:38:in `call'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:191:in `block in serve'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `fork'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:161:in `serve'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:131:in `block in run'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `loop'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application.rb:125:in `run'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-1.7.2/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /home/alexandre/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

我所有的类都有相同的问题,但我要重申的是,应用程序完美运行。 我在Mac OS上进行开发,应用程序在Debian 8上生产运行。 我的模型名称都是正确的,我已经验证过了。 谢谢


如果您键入 ::User.all,它是否有效? - xuanduc987
1
谢谢,但是在新的ssh会话后它可以工作。 - Alexandre
突然出现了同样的问题。不知道发生了什么。 - rewolf
2个回答

106

我遇到了这个问题,发现是在对一个工作文件进行调整后发生的。解决方法是重新启动Spring加载器。只需运行

spring stop

那么下次运行 rails console 时,它应该像往常一样加载东西。


3
你真是个救星!我被这个问题困扰了三个多星期。你真的为我解决了它 :) - Jagjot
1
@JagjotSingh 很抱歉你没有早点看到它!它也让我困惑了很长时间!但现在很高兴它能帮助到你了! - rewolf
这个问题让我头发都快白了。谢谢。 - a2f0
1
这个问题困扰了我一段时间,但我的问题真的很随机,比如,等待1分钟再回来就会自动解决,但今天我真的需要尽快解决它,非常感谢。从长远来看,有几个解决方案:1- 在生产环境中禁用它 https://github.com/rails/spring#temporarily-disabling-spring 或使用bundle的组 http://bundler.io/v1.12/groups.html。 我非常确定它会覆盖Gemfile.lock,但显然我只是个傻瓜(我被告知太多次了xd)。 - Alexis
你是个救命恩人啊!你就是! - hugalves
1
就我个人而言,“spring stop” 对我没有用,但是我通过“ps -ef | grep spring”命令找到了一个Spring进程。在杀掉该进程后,控制台如上所述成功加载。 - johnsampson

3

我曾经也遇到过这个问题,rewolf的答案暂时解决了我的问题

补充他的回答:

运行下面的命令停止spring gem:

spring stop

您可以通过将spring gem(Spring框架)从您的bin/可执行文件中移除,来永久解决此问题。
bin/spring binstub --remove --all

或者

spring binstub --remove --all

现在,您可以运行以下命令以进入生产环境中的rails控制台

rails c --environment=production

此外,为避免在后续情况下发生此类问题,请确保在Gemfile中仅在developmenttest组中存在spring gem。

此外,在生产环境中,请始终向bundle install命令提供--without development test参数。

bundle install --without development test

而不是通常或普通的

bundle install

请注意:当您运行rails crails console命令时,如果看到以下输出:

Running via Spring preloader in process 26651 WARNING: Spring is running in production. To fix this make sure the spring gem is only present in development and test groups in your Gemfile and make sure you always use bundle install --without development test in production

这意味着spring宝石正在您的生产环境中运行,应该停止或完全从bin可执行文件中删除。
就是这样。
希望能帮到您。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接