Rails console in production: NameError: uninitialized constant

Rails console in production: NameError: uninitialized constant

我有一个 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 上生产。 我的模型已正确命名,我已验证。 谢谢

我遇到了这个问题,并在我对我的一个工作文件进行了调整后意识到它发生了。 修复它的是重新启动 spring 加载程序。刚刚 运行

spring stop

那么下次你 运行 rails console 它应该会正常加载。

我遇到了同样的问题,上面的 为我解决了 暂时

补充一下他的回答:

通过运行以下命令停止spring gem后

spring stop

您还可以永久解决此问题,方法是向上 springing(删除 spring gem)您的 bin/ 可执行文件:

bin/spring binstub --remove --all

spring binstub --remove --all

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

rails c --environment=production

此外,为了避免这种情况在随后的情况下发生,请努力确保 spring gem 仅出现在您的 Gemfile 中的 developmenttest 组中.

此外,当您在生产环境中时,确保始终向 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 gem 在您的生产环境中 运行ning,应该将其停止或从您的 bin 可执行文件中完全删除。

就这些了。

希望对您有所帮助