Rails - why would a model inside RAILS_ROOT/lib not be available in production mode?
I have a class l开发者_JAVA百科ocated 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.
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.
You might need to check differences between the configuration settings between development and production environment:
config/environments/production.rb
and config/environments/development.rb
.
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"]
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.
(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.)
精彩评论