[Ruby] [Beginner] Setting environment variables -dotenv-rails-

less than 1 minute read

What are environment variables?

Environment variables are like boxes for information that you can’t upload to Github. This allows you to write passwords directly in the application, By passing data from the OS, you can run the application without worrying about information leakage.

Setting method

Install gem

Gemfile


gem'dotenv-rails'
$ bundle install

#####.env file creation In the root directory where the Gemfile is located create .env file

Create environment variable
KEY='*******' # Enter the key or password you want to use
SECRET_KEY='*******'

When calling

ENV['KEY']

Then you can call it.

Exclude from Git management

.gitignore


/.env

If you forget this, information will be leaked, so be sure to set it.