Rails:未初始化的常量仅发生在生产服务器上 [英] Rails: uninitialized constant just happen on production server
问题描述
我有一个放在lib/network
内的类:
module NetworkApi
class NetworkProxy
end
end
然后在另一堂课中,我引用了该课:
Then in another class, I referenced this class:
network_proxy = ::NetworkApi::NetworkProxy.new(params)
一切都可以在我的开发环境上正常运行,但是当我部署到服务器时,在上一行出现了一条错误消息:
Everything runs normally on my development environment, but when I deploy to the server, I get an error at the above line with the message:
NameError: uninitialized constant NetworkApi::NetworkProxy
我不知道为什么会发生这种奇怪的错误.请告诉我为什么.
I don't know why this strange error happens. Please tell me why.
推荐答案
Note that Rails 5 disables autoloading after booting the app in production.
来自链接的博客文章:
在极少数情况下,我们的应用程序仍需要在生产环境中自动加载,我们可以通过将
enable_dependency_loading
设置为true
来启用它,如下所示:
In the rare situation where our application still needs autoloading in the production environment, we can enable it by setting up
enable_dependency_loading
totrue
as follows:
# config/application.rb
config.enable_dependency_loading = true
config.autoload_paths << Rails.root.join('lib')`
这篇关于Rails:未初始化的常量仅发生在生产服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!