Secrets.yml File in Ruby on Rails

Secrets.yml File in Ruby on Rails

You maybe have noticed a file called secrets.yml in the config directory of a Ruby on Rails 4.1 project. This feature was added as part of Rails 4.1 in order to have a common storage location for the keys and credentials for various services. You can use the secrets.yml for everything from AWS credentials to your secret_key_base (the default in Rails 4.1).

secrets.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
development:
  secret_key_base: super_long_secret_key_for_development
  active_merchant_login: 112233
  active_merchant_password: super_secret_password

test:
  secret_key_base: super_long_secret_key_for_test
  active_merchant_login: 112233
  active_merchant_password: super_secret_password

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  active_merchant_login: <%= ENV["AM_LOGIN"] %>
  active_merchant_password: <%= ENV["AM_PASSWORD"] %>

You have add this file to your .gitignore file to avoid accidently pushing your keys to git. You can also store your production keys in this file if you wish.

To access the various keys in the secrets.yml file:

1
Rails.application.secrets.key_name

Example: The following code will returns the merchant login.

1
Rails.application.secrets.active_merchant_login # returns 112233 on development/test

So far so good, That’s it! See ya! :)