How To Use Rails Credentials
As any strong framework always need to take care of security Rails know as one of the best framework for ruby, the purpose of credentials is to secure your important information from being exposed to the public or being pushed to Github by mistake. For example like secure Google Map token Id. Today I will show you how to secure your keys and how to use them in rails app
Encrypted Credentials
To store your keys in credentials for first time you have to open console in your rails app and run this code
EDITOR=nano rails credentials:edit
You can change nano to vim
or any other editor you like, it’s all up to you, after you will see edit opens like this
Now you can add anything key value pair and save it to use later
As you my noticed I added a key value pair, after you done editing you save your file and you will get a message saying
File encrypted and saved.
Now you saved your file you will notice a file created called master.key
has a private key you must not push it to git because with that key every one can open your credentials, save it in safe place to use later
Reading the Credentials
To read your secret keys or use it inside your rails app it’s very easy and straight forward, all what you have to do is to add this code anywhere in your app
Rails::Application.credentials.medium