在 Rails 应用程序中使用可挂载引擎时,有必要将此引擎挂载到父应用程序 config/routes.rb
文件中,如下所示:
mount MyEngine::Engine, at: "/my_engine"
但是,是否也有可能以某种方式动态将引擎安装在父应用程序中,例如在使用 bundle install
安装引擎期间的初始化程序调用中?
最佳答案
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace MyEngine
config.my_engine = ActiveSupport::OrderedOptions.new
initializer 'my_engine.configuration' do |app|
if app.config.my_engine[:mounted_path]
app.routes.append do
mount MyEngine::Engine => app.config.my_engine[:mounted_path]
end
end
end
end
end
然后在config/application.rb
中的主应用中,可以设置
config.my_engine.mounted_path = "/some_path_here"
关于ruby-on-rails - 如何自动/动态挂载 Rails 引擎?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759105/