Rails - 为什么 RAILS_ROOT/lib 内的模型在生产模式下不可用?
我有一个位于 RAILS_ROOT/lib 文件夹内的类,我在我的一个助手中使用它,并且它在开发中效果很好。
当我切换到生产环境时,应用程序抛出一个 NameError (uninitializedconstant SomeHelper::SomeClass)
,我必须在助手中手动加载它:
load "#{Rails.root}/lib/some_class.rb"
module SomeHelper
def some_method
sc = SomeClass.new
# blah
end
end
我的印象是 RAILS_ROOT/lib/ 中的所有内容* 应该对应用程序可用 - 我需要配置什么才能在生产模式下发生这种情况吗?谢谢。
I have a class located inside RAILS_ROOT/lib folder, which I use in one of my helpers, and it works great in development.
When I switch to production, the application throws a NameError (uninitialized constant SomeHelper::SomeClass)
, and I have to load it manually in the helper:
load "#{Rails.root}/lib/some_class.rb"
module SomeHelper
def some_method
sc = SomeClass.new
# blah
end
end
I was under the impression that everything inside RAILS_ROOT/lib/* should be available all to the app - is there anything I need to configure to make this happen in prod mode? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当你调用 SomeHelper::SomeClass 时,Rails 的自动加载机制将尝试加载 lib/some_helper/some_class.rb 处的文件。Rails
不会加载 lib/* 中的所有内容,它只会在发生 ConstMissing 时尝试加载文件。
When you call SomeHelper::SomeClass, Rails' autoloading mechanism will try to load file at lib/some_helper/some_class.rb
Rails won't load everything in lib/*, it will only try to load files when ConstMissing occurs.
您可能需要检查开发环境和生产环境之间的配置设置之间的差异:
config/environments/development.rb 和 config/environments/development.rb。
在 Rails 初始化例程中,会调用
load_plugins()
来加载config.plugin_paths
中的所有插件。您需要确保包含您的文件夹lib/
,例如除了
config.plugin_paths
之外,您还可以命名应该命名的插件加载到config.plugins
中。如果该变量包含:all
则将加载所有(找到的)插件。(顺便说一句:与任一环境相同的配置设置应位于 config/environment.rb 中。环境之间的任何差异均取决于相应 .rb 文件中的设置。)
You might need to check differences between the configuration settings between development and production environment:
config/environments/production.rb
andconfig/environments/development.rb
.During the Rails initialization routine,
load_plugins()
is called which loads all plugins inconfig.plugin_paths
. You need to make sure that your folderlib/
is included, like inIn addition to
config.plugin_paths
, you can also name the plugins that should be loaded inconfig.plugins
. If that variable contains:all
then all plugins (found) will be loaded.(By the way: configuration settings equal to either environment should go in
config/environment.rb
. Any differences between enviroments are due to settings in the respective.rb
files.)