Search code examples
ruby-on-railsserverpuma

Rails: How to set secret key base on windows


I am trying to deploy an application to production but I get the following error

<RuntimeError: Missing `secret_key_base` for 'production' environment, set this value in `config/secrets.yml`>

I checked the config/secrets.yml file and saw this

production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

I know how to generate a new key, but where do I store it so that it can be picked-up by "<%= ENV["SECRET_KEY_BASE"] %>"?

I am currently using PUMA server and Windows 7 for this.


Solution

  • I just ran into the same problem today. After some digging around I had it figured out.

    According to this site, you can set your own environment variable by adding a .yml file then have your application.rb read the file.

    I created a local_env.yml in /config and added the following code.

    SECRET_KEY_BASE: your_key
    

    Then in /local/application.rb add the following code.

    class Application < Rails::Application
        .
        .
        .
        config.before_configuration do
            env_file = File.join(Rails.root, 'config', 'local_env.yml')
            YAML.load(File.open(env_file)).each do |key, value|
                ENV[key.to_s] = value
            end if File.exists?(env_file)
        end
    end
    

    You will probably want to add the local_env.yml to your .gitignore since it contain your key.

    OR if you feel lazy....instead of <%= ENV["SECRET_KEY_BASE"] %>, just put your key there and .gitignore the secrets.yml