Skip to main content

Devise Secret Key was not set automatically

Gems / Libraries • Asked by Jacob Hedengren
Hi everyone,

Strange problem appeared today. I am using Ruby 2.5.1 and Rails 2.5.0.rc2. When installing devise and pushing to heroku I got an error saying that the devise secret key was not set even tough the devise installation and db:migrate succeeded. I then had to check my build logs to find the key and manually add it to my devise initializer. Whats the reason causing this problem? Is it safe to have my secret key in the initializer? Some way to avoid this in the future?

Thanks a lot in advance!

/ Jacob

In Rails 5.2, you have the new credentials file and you must set your credentials secret_key_base to the Devise secret key.


config.secret_key = Rails.application.credentials.secret_key_base

You'll need to put your RAILS_MASTER_KEY env variable in as well so your app can decrypt the credentials file.

I'm guessing they'll fix this in a new release soon, but for now this is the solution I used. 👍

Thanks Chris!

So 

config.secret_key = Rails.application.credentials.secret_key_base

is what I put in my devise initializer and thats it?

How do I put my RAILS_MASTER_KEY env variable in as well? Do I put 

config.require_master_key = ?

in my devise initializer as well or set it to true in my production.rb?

Thanks a lot for the help, I appreciate that!

So your RAILS_MASTER_KEY is an environment variable you add to the Heroku admin in the Environment Variables section. You don't ever want that in your repository.

The secret_key line needs to go in your initializer replacing the one you manually added.

Hey, I'm running into the same problem. It works on my main computer on a app i just started working on this week (Rails 5.2.0, Devise 4.4.3, Ruby 2.5.1) but when I cloned the repo onto my home computer, I am getting this error.

Running 

Rails.application.credentials.secret_key_base

Rails.application.secrets.secret_key_base

Rails.application.config.secret_key_base

They all return NIL. Should I be setting them manually now?

Opps my bad. Realised I didn't have the master.key in my cloned system. Added it all and all works with Devise 4.4.3. No edits needed.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.