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

时间:2017-01-10 02:26:00

标签: ruby-on-rails ruby autoload

我有一个课程,我放在lib/network

module NetworkApi
  class NetworkProxy
  end
end

然后在另一个班级中,我引用了这个类:

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

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

NameError: uninitialized constant NetworkApi::NetworkProxy

我不知道为什么会发生这种奇怪的错误。请告诉我原因。

1 个答案:

答案 0 :(得分:9)

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')`