问题
I have a class that I put inside 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.
回答1:
Note that Rails 5 disables autoloading after booting the app in production.
From the linked blog post:
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')`
来源:https://stackoverflow.com/questions/41560160/rails-uninitialized-constant-just-happen-on-production-server