There are two ways to access secret_key_base:
- Rails.application.credentials.secret_key_base
- Rails.application.secrets.secret_key_base
Rails 5 took the first way by default.
you can change Rails.application.credentials.secret_key_base
by rails credentials:edit
. for all other environments, remember to set environment variable RAILS_MASTER_KEY
to be the same content of config/master.key
. the master.key
is git ignored by default. this way uses the same secret key for all environments. if you want to use different keys, you need to control namespaces by yourself.
If you prefer the second way Rails.application.secrets.secret_key_base
. you need to create config/secrets.yml
:
development:
secret_key_base: ...
test:
secret_key_base: ...
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
remember to set environment variable SECRET_KEY_BASE
on production.
if config/secrets.yml
file is secret enough, changing <%= ENV["SECRET_KEY_BASE"] %>
to plain text is fine.
rake secret
can generate a random secret key for you.
I prefer the second way(old way), because of simple.
production
anddevelopment
breakdown so I can specify them separately? – Wivinah<%= ENV.fetch("SECRET_BASE_KEY") %>
. You can usedirenv
ordotenv
to make setting the env vars less of a hassle. If you are using docker you can set ENV vars via the container. – Kelda