There are many ways to set environment variables.
Here are two of them,
Option one: set ENV variables via yml file
Create a file config/local_env.yml:
config / local _env.yml:
SAMPLE_APP_DATABASE_USER: 'your username'
SAMPLE_APP_DATABASE_PASSWORD: '******'
Above are the names that you will use, for example . these may be names as you wish . you can take any name, but we must use the same name in the ENV link . ENV['SAMPLE_APP_DATABASE_USER']
add it to gitignore:
/config/local_env.yml
Change the code in application.rb
application.rb:
config.before_configuration do
env_file = File.join(Rails.root, 'config', 'local_env.yml')
YAML.load(File.open(env_file)).each do |key, value|
ENV[key.to_s] = value
end if File.exists?(env_file)
end
config/local_env.yml, / .
:
username: <%= ENV['SAMPLE_APP_DATABASE_USER'] %>
password: <%= ENV['SAMPLE_APP_DATABASE_PASSWORD'] %>
: Figaro Gem
Rubys , . config/application.yml , - Rails.
. Gemfile :
gem 'figaro'
bundle install
:
$ bundle exec figaro install
config/application.yml .gitignore file, git.
/ config/application.yml:
SAMPLE_APP_DATABASE_USER: 'your username'
SAMPLE_APP_DATABASE_PASSWORD: '******'
ENV:
ENV["SAMPLE_APP_DATABASE_USER"]
.