1
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 17, 2016 at 11:30
\$\endgroup\$
4
  • \$\begingroup\$ Best Practice would be to have ENV Variables from shell. VAR=value rails server. \$\endgroup\$ Commented Oct 17, 2016 at 11:32
  • \$\begingroup\$ What if you have different credentials for username and password or database_name configuration ? \$\endgroup\$ Commented Oct 17, 2016 at 11:34
  • \$\begingroup\$ What're you thinking? Production/Development? \$\endgroup\$ Commented Oct 17, 2016 at 11:38
  • \$\begingroup\$ (in case of setting ENV Variables from shell) what if you have more than one app? I guess the username and password would be the same but the database_name could be different on each case, right? \$\endgroup\$ Commented Oct 17, 2016 at 11:43

2 Answers 2

2
\$\begingroup\$

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

answered Oct 17, 2016 at 11:43
\$\endgroup\$
2
  • \$\begingroup\$ so, everything I have in config/application.yml should move to config/secrets.yml, right? so the ENV variables should be in another .yml file likw I have or in shell ? \$\endgroup\$ Commented Oct 17, 2016 at 11:49
  • \$\begingroup\$ yes, move data from application.yml to secret.yml. ENV variables you need in HEROKU, AWS etc, but for example in DigitalOcean or in local server you can work with secrets.yml \$\endgroup\$ Commented Oct 17, 2016 at 13:36
1
\$\begingroup\$

An Alternative is ..

Add gem 'config' to your Gemfile and run bundle install to install it. Then run

rails g config:install

which will generate customizable config file config/initializers/config.rb and set of default settings files:

config/settings.yml
config/settings/development.yml
config/settings/production.yml
config/settings/test.yml

You can now edit them to adjust to your needs. a

see the usage in https://github.com/railsconfig/config compatible Rails>= 3.1, 4 and 5

answered Oct 17, 2016 at 11:58
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.