Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails console won't load environment variables in secrets.yml

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?

like image 808
mltsy Avatar asked Apr 24 '17 15:04

mltsy


People also ask

How do I see environment variables in rails?

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.

Are environment variables safe for secrets?

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.


1 Answers

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.

like image 183
mltsy Avatar answered Oct 05 '22 22:10

mltsy