Spring and Foreman complaining about secret_key not being set

Spring and Foreman when freshly set up can complain about secret_key not being set when it is, I will show you how to fix this

Spring and Foreman don’t always see eye to eye, and if you spin up a new rails 5 app rails new my_new_app then by default you will get Spring installed.

Having Spring installed is a good thing as it helps speed up development, if you don’t fully understand what Spring and Foreman are doing though, you end up spending some time scratching your head.

When you add Foreman to your project with gem install foreman certain things will work (foreman start worked perfectly for me) but some things like foreman run rails g... will shout about ENVs not being set that you know for a fact are set.

I realised that this is likely because Spring is running and doesn’t have access to the ENV variables that you set since before starting the project.

spring stop then trying my Foreman command again worked perfectly for me.

Here is a some annotated output of what was happening in my case, hopefully it helps you;

// This is the command I was trying to run

foreman run rails g model Project name:string description:text required_by:date

// Here is the output I was getting
// Note - I have a secret key set up
// and it is working on dev/prod servers

/Users/tosbourn/.rvm/gems/ruby-2.3.1/gems/devise-4.2.0/lib/devise/rails/routes.rb:498:in `raise_no_secret_key`: Devise.secret_key was not set. Please add the following to your Devise initializer:

  config.secret_key = '81559f31579...5e48f4efbf3eedf98700d5b8cda9e9bd579'

Please ensure you restarted your application after installing Devise or setting the key.

// I have cut out some of the output
// it was just some stack traces that
// are not relevant

// Command I typed to stop Spring

spring stop

// Output from command

Spring stopped.

// I retried my original command

foreman run rails g model Project name:string description:text required_by:date
Running via Spring preloader in process 55277
      invoke  active_record
      create    db/migrate/20160821093254_create_projects.rb
      create    app/models/project.rb
      invoke    test_unit
      create      test/models/project_test.rb
      create      test/fixtures/projects.yml

// Yay!

Recent posts View all

Web DevProductivity

Keeping on top of website updates

Learn what website updates are, what they entail, why they are important, and how we can help

Freelancing

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.