问题
I'm having some problem accessing Rails.root
from my rails engine, that I'm creating. I need to fetch a yml config file from the main app.
Is there any "best practices" for handling configurations for your engines?
回答1:
Let's assume you have a module attribute for that.
# lib/my_engine.rb
module MyEngine
mattr_accessor :app_root
end
Then you can load it from the initialize block like so:
# lib/my_engine/engine.rb
module MyEngine
class Engine < Rails::Engine
initializer "my_engine.load_app_root" do |app|
MyEngine.app_root = app.root
end
end
end
回答2:
Instead use Rails.root
use: MyEngine::Engine.root
;D
来源:https://stackoverflow.com/questions/11615635/rails-root-from-engine