Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4.1 environment variables not reloading

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?

like image 909
vich Avatar asked Nov 20 '25 20:11

vich


1 Answers

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

like image 74
Frederick Cheung Avatar answered Nov 23 '25 11:11

Frederick Cheung



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!