Back

Passing configs to rails engines

When building a Rails engine that can integrate into another Rails application, you might want to pass configuration parameters from the host that can be used in the engine.

In the engine (lets call it HelpEngine)

Add the setup and config class methods to the module.

help_engine/lib/help_engine/engine.rb

module HelpEngine
  class Engine < ::Rails::Engine
    isolate_namespace MyApp
  end

  def self.setup(&block)
    @config ||= MyApp::Engine::Configuration.new
    yield @config if block
    @config
  end

  def self.config
    Rails.application.config
  end
end

You can then access the configuration parameter from the engine using:

HelpEngine.config.testing_var

In the main app (host)

config/initializers/help_engine.rb

HelpEngine.setup do |config|
  config.testing_var = "asdfasdf"
end