ruby-on-railsdeploymentcapistrano

ruby on rails ArgumentError: Missing `secret_key_base` for 'production' environment


I'm using Rails 7.0.6, and Ruby 3.2.2. I have a local machine and a remote server. When I deploy with Capistrano by typing :

cap production deploy

I get this error :

rake stdout: Nothing written
rake stderr: rake aborted!
ArgumentError: Missing `secret_key_base` for 'production' environment, set this string with `bin/rails credentials:edit`

I was following this tutorial : Deploy Ruby on Rails

Solutions I tried :

-I added file name .rbenv-vars in the server app folder with those contents :

DATABASE_URL = postgresql://username:pass@127.0.0.1/myapp_production
SECRET_KEY_BASE = secret key here by typing in local machine "bundle exec rails secret"

but that didn't work.

-I also typed in terminal :

nano bin/rails credentials:edit 

and then a file opened with this contents :

#!/usr/bin/env ruby
APP_PATH = File.expand_path("../config/application", __dir__)
require_relative "../config/boot"

require "rails/commands"

and I added this line :

secret_key_base = secret_key_here ******

but that didn't work either.Any solutions?


Solution

  • First generate the secret key :

    rails secret 
    

    then type :

    EDITOR=nano rails credentials:edit --environment production
    

    then paste the secret key there and type :

    secret_key_base : key_here
    

    then commit and push to github, and after that go to the server side and create this file :

    nano .rbenv-vars
    

    in app root folder, and add this info:

    DATABASE_URL=postgresql://user_name:PASSWORD@127.0.0.1/app_name
    
    RAILS_MASTER_KEY= you'll find the master key in local machine in "config/credentials/production.key
    
    SECRET_KEY_BASE= same as the one you generated using "rails secret"