Rails.root from engine

萝らか妹 提交于 2019-12-06 00:26:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!