instance method config_for

Ruby on Rails 6.0.6

Since v4.2.9

Available in: v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

config_for(name, env: Rails.env)

Convenience for loading config/foo.yml for the current Rails env.

Example:

# config/exception_notification.yml:
production:
  url: http://127.0.0.1:8080
  namespace: my_app_production
development:
  url: http://localhost:3001
  namespace: my_app_development

# config/environments/production.rb
Rails.application.configure do
  config.middleware.use ExceptionNotifier, config_for(:exception_notification)
end

Parameters

name req
env key = Rails.env
Source
# File railties/lib/rails/application.rb, line 222
    def config_for(name, env: Rails.env)
      yaml = name.is_a?(Pathname) ? name : Pathname.new("#{paths["config"].existent.first}/#{name}.yml")

      if yaml.exist?
        require "erb"
        all_configs    = YAML.load(ERB.new(yaml.read).result) || {}
        config, shared = all_configs[env], all_configs["shared"]

        config ||= {} if shared.nil? || shared.is_a?(Hash)

        if config.is_a?(Hash)
          ActiveSupport::OrderedOptions.new.update(NonSymbolAccessDeprecatedHash.new(shared&.deep_merge(config) || config))
        else
          config || shared
        end
      else
        raise "Could not load configuration. No such file - #{yaml}"
      end
    rescue Psych::SyntaxError => error
      raise "YAML syntax error occurred while parsing #{yaml}. " \
        "Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
        "Error: #{error.message}"
    end

Defined in railties/lib/rails/application.rb line 222 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Rails::Application

Type at least 2 characters to search.

Use the arrow keys to navigate results, Enter to open one, Escape to close.

Keyboard shortcuts

/
Focus search
⌘K / Ctrl-K
Command palette
?
This help
Esc
Close