3

In config/data.yml, I have:

development:
  something: some_value
production:
  something: different_value

in config/initializers/constants.rb, I load it:

CONST_DATA = YAML.load_file("#{::Rails.root}/config/data.yml")[::Rails.env]

and then I want to use this CONST_DATA in the /config/environments/production.rb file, but when I do that, I get an error saying

ERROR -- : uninitialized constant CONST_DATA (NameError)

How to make this constant accessible in the /config/environments/production.rb file?

Thank you

user984621
  • 46,344
  • 73
  • 224
  • 412
  • The initializers are run only after the environment is set up. But of course you can load your constant explicitly at the top of `production.rb` using e.g. `require 'config/initializers/constants'`. – Raffael May 28 '16 at 23:15
  • Also, including the line with `load_file` directly in your production.rb or application.rb would be fine I guess. – Raffael May 28 '16 at 23:16

2 Answers2

0

Since the release of Rails 4.1 there's a file called config/secrets.yml. This data is loaded and available during initialization. You can then access keys in it like this:

Rails.application.secrets.key_name
Weston
  • 461
  • 3
  • 8
0

Config gem helps you easily manage environment specific settings in an easy and usable manner.

Following link explains about how to define custom configuration variables in rails:

How to define custom configuration variables in rails

Community
  • 1
  • 1
Mahesh Khond
  • 1,297
  • 1
  • 14
  • 31