In rails 5, I used to create a file called .env
in an app's root directory, add something like this to it:
MY_VARIABLE=1234
and access the variables inside the app with ENV["MY_VARIABLE"]
But in a rails 6 app I do so same thing and it returns nil
. When I run ENV
I see lots of environment variables, but none which were in .env
.
I am not sure why this would be?
Run spring stop
and restart your rails app. However, if that didn't work, I like to let you know that Rails 6 now supports multiple credentials. You can use the format below:
From your rails terminals, run EDITOR=vim rails credentials:edit
. This will create credentials.yml.enc
and master.key
file for you inside /config
if you don't have one.
Then you can put your credentials in a YAML file like this
development:
aws:
access_key_id: 123
secret_access_key: 345
production:
aws:
access_key_id: 1f3649fe-ebbd-11e9-81b4-2a2ae2dbcce4
secret_access_key: 203060d3a5456fa6cd2da3c958001440
You can fetch the credentials this way
> Rails.application.credentials[Rails.env.to_sym][:aws][:access_key_id]
#=> "123"
Then you can proceed to bundle install
and run rails server
You can read up from this link on how you can fetch your credentials.