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

1.7k Views Asked by At

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
[email protected]

And from the console:

learn-rails :001 > ENV["GMAIL_USERNAME"]
 => "[email protected]"

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"] %>
2

There are 2 best solutions below

0
tegdude On

Try:

Rails.application.secrets['email_provider_username']
2
Empact On

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.