Rails - 为什么 RAILS_ROOT/lib 中的模型在生产模式下不可用? [英] Rails - why would a model inside RAILS_ROOT/lib not be available in production mode?
问题描述
我在 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.
当我切换到生产时,应用程序抛出一个 NameError(未初始化的常量 SomeHelper::SomeClass)
,我必须在帮助器中手动加载它:
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
我的印象是 RAILS_ROOT/lib/* 中的所有内容都应该对应用程序可用 - 我需要配置什么才能在生产模式下实现这一点吗?谢谢.
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.
推荐答案
您可能需要检查开发环境和生产环境之间的配置设置之间的差异:config/environments/production.rb
和 config/environments/development.rb
.
You might need to check differences between the configuration settings between development and production environment:
config/environments/production.rb
and config/environments/development.rb
.
在 Rails 初始化例程中,会调用 load_plugins()
来加载 config.plugin_paths
中的所有插件.您需要确保包含您的文件夹 lib/
,如
During the Rails initialization routine, load_plugins()
is called which loads all plugins in config.plugin_paths
. You need to make sure that your folder lib/
is included, like in
config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
除了config.plugin_paths
,您还可以在config.plugins
中命名应该加载的插件.如果该变量包含 :all
,则将加载所有插件(找到).
In addition to config.plugin_paths
, you can also name the plugins that should be loaded in config.plugins
. If that variable contains :all
then all plugins (found) will be loaded.
(顺便说一句:与任一环境相同的配置设置应该放在 config/environment.rb
中.环境之间的任何差异都是由于相应的 .rb
中的设置造成的文件.)
(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.)
这篇关于Rails - 为什么 RAILS_ROOT/lib 中的模型在生产模式下不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!