Rails: uninitialized constant just happen on production server

大兔子大兔子 提交于 2019-12-19 19:56:03

问题


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 to true 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!