I've followed the steps from this video from railscasts for setting ENV
variables.
This is my configuration:
config/database.yml
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
socket: /var/run/mysqld/mysqld.sock
development:
<<: *default
database: <%= ENV['DATABASE_NAME'] %>
test:
<<: *default
database: <%= ENV['DATABASE_NAME'] %>
production:
<<: *default
database: <%= ENV['DATABASE_NAME'] %>
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
config/application.rb
config = YAML.load(File.read(File.expand_path('../application.yml', __FILE__)))
config.merge! config.fetch(Rails.env, {})
config.each do |key, value|
ENV[key] = value.to_s unless value.kind_of? Hash
end
config/application.yml
development:
DATABASE_USERNAME: "username goes here"
DATABASE_PASSWORD: "password goes here"
DATABASE_NAME: "database name goes here"
SECRET_KEY_BASE: "secret key goes here"
test:
DATABASE_USERNAME: "username goes here"
DATABASE_PASSWORD: "password goes here"
DATABASE_NAME: "database name goes here"
SECRET_KEY_BASE: "secret key goes here"
production:
DATABASE_USERNAME: "username goes here"
DATABASE_PASSWORD: "password goes here"
DATABASE_NAME: "database name goes here"
SECRET_KEY_BASE: "secret key goes here"
Does this configuration follow best practices, or need some changes?
VAR=value rails server
. \$\endgroup\$ – Frederik Spang Oct 17 '16 at 11:32