3

I created an application.yml file in the config dir and added some key-value pairs (i.e. AWS_REGION: us-east-1). I also added the following to application.rb to read the file and update the ENV hash:

if Rails.env.development?  
  config.before_configuration do
    env_file = File.join(Rails.root, 'config', 'application.yml')
    YAML.load(File.open(env_file)).each do |key, value|
      ENV[key.to_s] = value
    end if File.exists?(env_file)
  end
end

Then, if I run rails c I can see the ENV variables I set in my application.yml file (i.e. ENV['AWS_REGION']). However, if I update any of the values or even add a new key-value pair to the file, on the next time I run rails c the values aren't updated and the new keys aren't available.

I tried restarting Terminal but it didn't work. Any suggestions?

vich
  • 11,836
  • 13
  • 49
  • 66

2 Answers2

2

It looks like the problem that spring caches the state of the app as it starts up - it doesn't know that changes to config/application.yml should result in reloading the app

You can customise how spring watches and reloads your app by creating config/spring.rb - it looks like adding something like

Spring.watch "config/application.yml"

is what you're after

Frederick Cheung
  • 83,189
  • 8
  • 152
  • 174
  • 3
    I had to run `bin/spring stop` to get this to take effect. http://stackoverflow.com/a/26323727/1011746 – mindriot Dec 10 '15 at 20:19
2

remove gem 'spring' from Gemfile

thedanotto
  • 6,895
  • 5
  • 45
  • 43