Rails 4.1 environment variables not reloading

2019-07-23 08:35发布

问题:

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?

回答1:

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



回答2:

remove gem 'spring' from Gemfile