Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails ENV Variables

I have recently asked a similar question to this but as the problem has moved on slighty I have decided to create a new question - I hope this is the expected approach?

Having pushed my Rails 4 app to Heroku I keep getting an Internal Server Error Page and the error is:

You must set config.secret_key_base in your app's config

This is happening because my .gitignore file includes the config/initializers/secret_token.rb deliberately.

I have installed the Figaro gem so that I could set my secret_key_base as an environment variable for added security. I have checked on Heroku that the key has been set correctly.

My code for the secret_token.rb is as follows:

MyApp::Application.config.secret_key_base = ENV["SECRET_KEY_BASE"]

However, I'm still getting the same issue.

Can anyone help???

like image 269
tommyd456 Avatar asked Sep 01 '13 10:09

tommyd456


People also ask

Where are Ruby environment variables stored?

You store separate environment variables in config/development. rb , config/testing. rb and config/production. rb respectively.

How do I use an environment variable in Ruby?

Passing Environment Variables to Ruby To pass environment variables to Ruby, simply set that environment variable in the shell. This varies slightly between operating systems, but the concepts remain the same. To set an environment variable on the Windows command prompt, use the set command.


1 Answers

I did something simlilar to you that worked, but didn't use Figaro. I based it off this blog post

In summary, here's what I did:

1) remove config/initializers/secret_token.rb from your .gitignore

2) Use this code for your secret_token.rb:

MyApp::Application.config.secret_token = if Rails.env.development? or Rails.env.test?
  ('x' * 30) # meets minimum requirement of 30 chars long
else
  ENV['SECRET_TOKEN']
end

3) commit and re-push

4) set Heroku env variale like:

heroku config:set SECRET_TOKEN=12345.....

Worked as soon as Heroku restarted after the config set.

like image 92
Josh Diehl Avatar answered Sep 27 '22 21:09

Josh Diehl