Environment variables are nil in Rails app (can't connect to Google Drive)

learn-ruby-on-rails

Solution

I had this problem and it was caused by spring running the server in the background even when I quit the server in the terminal.

If this is the cause in your case, you can fix it by running `spring stop`.

Problem

Working with the book Learn Ruby on Rails, I'm stuck on the section of the tutorial where you connect to Google Drive to save the form submission to a spreadsheet. I'm not able to authenticate w/ Google because `Rails.application.secrets.email_provider_username` and `Rails.application.secrets.email_provider_password` are nil. More specifically, it seems like my rails app isn't seeing any of my environment variables. I've verified that the variables are set properly: ``` ynkwinl-ujurvt0:learn-rails katie$ printenv | grep GMAIL_USERNAME GMAIL_USERNAME=kjXXXXXX@gmail.com ``` And from the console: ``` learn-rails :001 > ENV["GMAIL_USERNAME"] => "kjXXXXXX@gmail.com" ``` But trying to access it via Rails: ``` learn-rails :001 > Rails.application.secrets.email_provider_username => nil ``` The relevant line of secrets.yml: ``` email_provider_username: <%= ENV["GMAIL_USERNAME"] %> ``` I've been stuck on this for an hour and can't find an answer (the previous question on this topic covered a slightly different scenario). I've worked around it by hard coding my username/password into the `secrets.yml` file, but I'd like to understand what's going on for future reference. Full 'secrets.yml' file for reference: ``` development: email_provider_username: <%= ENV["GMAIL_USERNAME"] %> email_provider_password: <%= ENV["GMAIL_PASSWORD"] %> domain_name: example.com mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %> mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %> owner_email: <%= ENV["OWNER_EMAIL"] %> secret_key_base: very_long_random_string test: secret_key_base: very_long_random_string # Do not keep production secrets in the repository, # instead read values from the environment. production: email_provider_username: <%= ENV["GMAIL_USERNAME"] %> email_provider_password: <%= ENV["GMAIL_PASSWORD"] %> domain_name: <%= ENV["DOMAIN_NAME"] %> mailchimp_api_key: <%= ENV["MAILCHIMP_API_KEY"] %> mailchimp_list_id: <%= ENV["MAILCHIMP_LIST_ID"] %> owner_email: <%= ENV["OWNER_EMAIL"] %> secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> ```

Original source