Posted on / by admin / in Ruby On Rails

Rails, Capistrano, Ubuntu and its Freaking Secret Token

This is a good resources to deploy rails >4.1 to internet, however there are mystery need to be solved which is the secret key token, and it could be wasting your time just to make your development server farm up and running

so here are my steps after 2 x 24 hours figure it out ?????????(and also for my personal notes in case i need to deploy another apps)
First, after you follow the steps above make sure you encounter this error

An unhandled lowlevel error occured. The application logs may have details.

You could ssh to your server deploy@yourseverip and head to “/home/deploy/apps/your-app-name/current” and head to log directory, run tail -f puma.access.log make you’ll see

#<RuntimeError: Missing `secret_token` and `secret_key_base` for ‘staging’ environment, set these values in `config/secrets.yml`>

So there are two places that causing this problem

  • secrets.yml on your config folder
  • devise.rb on your initializer folder

They are both require secret token to work

Now head to your current application on the server folder and execute

bundle exec rake secret

This will allow rails to generate 30 hash character that will be used for the secret token, save that token somewhere on your local notes

Now head to your ~ folder and edit the .bashrc file

nano ~/.bashrc

Now put this line

export SECRET_KEY_BASE=“your 30 hash character that you saved before”

now head to your local machine and modify secret.yml file, put this line (the test: could depends on your environment could be staging, or production)

test: secret_key_base: <%= ENV[“SECRET_KEY_BASE”] %>

the last file we could bother is devise.rb on initializer if you utilize devise, put this line

config.secret_key = ENV[“SECRET_KEY_BASE”]

Don’t forget to push your code to repo and for final call, pick one that suit your environment

cap test deploy
cap staging deploy
cap production deploy

head to browser open your address/ip and this should save your day ❤️❤️❤️❤️????????


Leave a Reply