Rails: 未初始化的常量只发生在生产服务器上

Rails: uninitialized constant just happen on production server

我有一个 class 放在里面 lib/network:

module NetworkApi
  class NetworkProxy
  end
end

然后在另一个 class 中,我引用了这个 class:

  network_proxy = ::NetworkApi::NetworkProxy.new(params)

在我的开发环境中一切正常,但是当我部署到服务器时,我在上面一行收到错误消息:

NameError: uninitialized constant NetworkApi::NetworkProxy

我不知道为什么会出现这个奇怪的错误。请告诉我为什么。

请注意 Rails 5 disables autoloading after booting the app in production

来自链接博客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')`