Set ENV variables for Ruby on Rails I've followed the steps from this video from railscasts for setting ENV variables.

This is my configuration:



config/database.yml

default: &amp;default
  adapter: mysql2
  encoding: utf8
  pool: 5
  username: &lt;%= ENV['DATABASE_USERNAME'] %&gt;
  password: &lt;%= ENV['DATABASE_PASSWORD'] %&gt;
  socket: /var/run/mysqld/mysqld.sock

development:
  &lt;&lt;: *default
database: &lt;%= ENV['DATABASE_NAME'] %&gt;
test:
  &lt;&lt;: *default
database: &lt;%= ENV['DATABASE_NAME'] %&gt;
production:
  &lt;&lt;: *default
  database: &lt;%= ENV['DATABASE_NAME'] %&gt;
  username: &lt;%= ENV['DATABASE_USERNAME'] %&gt;
password: &lt;%= ENV['DATABASE_PASSWORD'] %&gt;


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?
for what you use config/application.yml instead use config/secrets.yml. It is already implemented in Rails. Than you can fetch your key by:

Rails.application.secrets


even more, add to config/application.rb variable SECRETS = Rails.application.secrets and now you can use it:

SECRETS[:my_secret_key]


read more IN RAILS DOCS