Ask A Question

Notifications

You’re not receiving notifications from this thread.

Devise Secret Key was not set automatically

Jacob Hedengren asked in Gems / Libraries
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
Reply
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. 👍
Reply
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!
Reply
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.
Reply
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?
Reply
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.
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.