Rails:未初始化的常量仅发生在生产服务器上 [英] Rails: uninitialized constant just happen on production server

查看:67
本文介绍了Rails:未初始化的常量仅发生在生产服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个放在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 to true as follows:

# config/application.rb 

config.enable_dependency_loading = true
config.autoload_paths << Rails.root.join('lib')`

这篇关于Rails:未初始化的常量仅发生在生产服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆