Search code examples
ruby-on-railsruby-on-rails-5ruby-on-rails-5.2

Rails: How to fix "Missing secret_key_base for 'production' environment"


I simply can't get past the message:

Missing `secret_key_base` for 'production' environment, set this string with `rails credentials:edit` (ArgumentError)

I have Rails 5.2.0, and ran

EDITOR=vim rails credentials:edit

and inside:

production:
   secret_key_base: xxxxxxxxxxxxxxxxxxxxxxx

Save and, in the terminal:

RAILS_ENV=production rails c

Am I missing something? I've restarted the server and got the same issue, but have no issue in development mode.


Solution

  • Rails v4/5

    Keep default the secrets.yml file

    # config/secrets.yml
    production:
      secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
      aws_secret: abcde
      some_password: abcdex
    
    development:
      secret_key_base: static_secret_key
      aws_secret: abcde
    
    test:
      secret_key_base: static_test_secret_key
    
    
    #not_indented: key for all env in once
    secret_key_base: global_key_for_all_env
    
    RAILS_ENV=production SECRET_KEY_BASE=production_test_key rails c
    

    Rails v5.2.0 add to production env below, check this LINK

    config.require_master_key = true    #config/environments/production.rb
    

    Rails v7 https://blog.assistancy.be/blog/how-to-store-credentials-in-rails-7/