我有一个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上运行。 我的模型正确命名我验证。 感谢
答案 0 :(得分:78)
我遇到了这个问题,并且在我对其中一个作业文件进行调整后发现了这个问题。 修复它的原因是重新启动弹簧加载器。跑吧
spring stop
然后,下次运行rails console
时,它应该正常加载。
答案 1 :(得分:2)
我遇到了同样的问题,上面的rewolf's answer为我临时解决了这个问题。
只需添加他的答案即可:
通过运行以下命令停止spring gem之后
spring stop
您还可以通过提升bin/
可执行文件(从中删除春季宝石)来永久解决此问题:
bin/spring binstub --remove --all
或
spring binstub --remove --all
您现在可以运行以下命令进入生产中的Rails控制台
rails c --environment=production
此外,为避免在以后的情况下发生这种情况,请尽力确保spring宝石仅出现在Gemfile的development
和test
组中。
此外,在生产中,请确保始终为--without development test
命令提供bundle install
参数
bundle install --without development test
而不是通常或常见的
bundle install
请注意:作为指示,每当您运行命令rails c
或rails console
时,您都会看到以下输出:
在进程26651中通过Spring预加载器运行 警告:Spring正在生产中。要解决此问题,请确保春季宝石仅出现在Gemfile的
development
和test
组中,并确保在生产中始终使用bundle install --without development test
这表明spring gem在您的生产环境中正在运行,应将其停止或完全从bin可执行文件中删除。
仅此而已。
我希望这会有所帮助