I've created a rails app (rails 4.1) from scratch and I am facing a strange problem that I am not able to solve.
Every time I try to deploy my app on Heroku I get an error 500:
Missing
secret_key_base
for 'production' environment, set this value inconfig/secrets.yml
The secret.yml file contains the following configuration:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
On Heroku I have configured an environment variable "SECRET_KEY_BASE" with the result of "rake secret" command. If I launch "heroku config", I can see the variable with the correct name and value.
Why am I still getting this error?
Thanks a lot
I'm going to assume that you do not have your
secrets.yml
checked into source control (ie. it's in the.gitignore
file). Even if this isn't your situation, it's what many other people viewing this question have done because they have their code exposed on Github and don't want their secret key floating around.If it's not in source control, Heroku doesn't know about it. So Rails is looking for
Rails.application.secrets.secret_key_base
and it hasn't been set because Rails sets it by checking thesecrets.yml
file which doesn't exist. The simple workaround is to go into yourconfig/environments/production.rb
file and add the following line:This tells your application to set the secret key using the environment variable instead of looking for it in
secrets.yml
. It would have saved me a lot of time to know this up front.I've created
config/initializers/secret_key.rb
file and I wrote only following line of code:But I think that solution posted by @Erik Trautman is more elegant ;)
Edit: Oh, and finally I found this advice on Heroku: https://devcenter.heroku.com/changelog-items/426 :)
Enjoy!
What I did : On my production server, I create a config file (confthin.yml) for Thin (I'm using it) and add the following information :
I then launch the app with
Work like a charm and then no need to have the secret key on version control
Hope it could help, but I'm sure the same thing could be done with Unicorn and others.
I have a patch that I've used in a Rails 4.1 app to let me continue using the legacy key generator (and hence backwards session compatibility with Rails 3), by allowing the secret_key_base to be blank.
I've since reformatted the patch are submitted it to Rails as a Pull Request
I had the same problem after I used the .gitignore file from https://github.com/github/gitignore/blob/master/Rails.gitignore
Everything worked out fine after I commented the following lines in the .gitignore file.
You can export the secret keys to as environment variables on the
~/.bashrc
or~/.bash_profile
of your server:And then, you can source your
.bashrc
or.bash_profile
:Never commit your secrets.yml