With Rails 5, if I create a file, config/environment_variables.yml, that contains
development:
MY_VAR: abcdef
What do I need to do to get the Rails environment to recognized that environment variable? Right now, when I go to my console (by typing "rails console") on my local machine, it isn't turning up anything ...
2.4.0 :001 > ENV['MY_VAR']
=> nil
If you don't wish to use a gem, you can place this in your config/application.rb
# Load application ENV vars and merge with existing ENV vars. Loaded here so you can use the values in initializers.
ENV.update YAML.load_file('config/application.yml')[Rails.env] rescue {}
Then your config/application.yml
file should look like:
development:
API_KEY: 12345
staging:
API_KEY: 67890
Be sure to restart your server, then you can access the variables as you desire with ENV['API_KEY']
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.