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

1

9

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?

Galvez answered 24/4, 2017 at 15:11 Comment(0)
G
33

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.

Galvez answered 24/4, 2017 at 15:11 Comment(6)
See this issue in the Spring repository: github.com/rails/spring/issues/490Galvez
Yeah Spring is annoying. This is why I start my rails applications with --skip-spring every time.Kristelkristen
Interesting, direnv (github.com/direnv/direnv) was not working properly because of that, and now it works! Thank youFlaggy
or bin/spring stop for n00bz like me.Boarder
Hi, Joe! You just saved me some frustration. Hope all is well!Tref
Haha, hey Brad! Happy to help ;DGalvez

© 2022 - 2024 — McMap. All rights reserved.