I am trying to debug a problem with secrets.yml
loading environment variables, by setting some environment variables in development and running rails c
to inspect things. When I load Rails.applications.secrets
this way, it is not picking up any of the environment variables I have set (namely, SECRET_KEY_BASE
)
If I run the application with the same environment variables set, it picks them up fine (I'm using RubyMine to run the application, but running rails c
from the terminal)
In my rails console, I can see the environment variable I've set using ENV['SECRET_KEY_BASE']
, but it doesn't show up in Rails.application.secrets
. Why?
Use command ENV in rails console. That will return a hash of your environmental values you can access. Alternatively, you can access your environmental variables from your apps root path using the same command and the variables will be returned formatted.
Environment variables are more secure than plaintext files, because they are volatile/disposable, not saved; i.e. if you set only a local environment variable, like "set pwd=whatever," and then run the script, with something that exits your command shell at the end of the script, then the variable no longer exists.
TL;DR: spring stop
It turns out, as has happened so many times when things aren't making any sense, Spring is the culprit! I solved this problem (thanks to a related discussion) by running spring stop
and then trying again, after which it worked perfectly!
Apparently Spring was caching the environment, or certain pieces of the Rails application, and neglecting to reload them when the environment variables changed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With