How to use environment variables in RubyOnRails

■ Articles in English ↓ [How to Use Environment Variables in Ruby On Rails] (https://qiita.com/alokrawat050/items/ff6dceec32baa0c8fa57)

What are environment variables?

Each application requires configuration settings such as email account credentials and API keys for external services. You can use environment variables to pass local configuration settings to your application. There are several ways to use environment variables in Ruby On Rails, and there are also gems like FigaroGem. For this article, I'll use the local_env.yml file to share how to implement it.

Keep environment variable files private

If you're using GitHub to store and share your code and your project is open source, any developer can access your code. If you're collaborating with your team using a private git repository that you don't want to share your personal information or API keys with the public, local settings may not be suitable for all members of your team.

Use local_env.yml file:

Use the standard YAML file format to create a simple file that contains the key / value pairs for each environment variable.

** Create config / local_env.yml file: **

MAIL_USERNAME: 'Your_Username'
MAIL_PASSWORD: 'Your_Username'

** Set to .gitignore ** If you created your application's git repository, your application's root directory should contain a file named .gitignore. Add the following line to the .gitignore file

/config/local_env.yml

** Set in Rails application file ** After setting the environment variables, the file "local_env.yml" needs to be set in "config / application.rb". Set the following code in the ** config / application.rb ** file

config.before_configuration do
  env_file = File.join(Rails.root, 'config', 'local_env.yml')
  YAML.load(File.open(env_file)).each do |key, value|
    ENV[key.to_s] = value
  end if File.exists?(env_file)
end

The above code sets environment variables from the local_env.yml file.

** Use environment variables in code ** You can use ENV ["MAIL_USERNAME"] in your Rails application. Example:

ActionMailer::Base.smtp_settings = {
    address: "smtp.gmail.com",
    enable_starttls_auto: true,
    port: 587,
    authentication: :plain,
    user_name: ENV["MAIL_USERNAME"],
    password: ENV["MAIL_PASSWORD"],
    openssl_verify_mode: 'none'
    }

Enjoy coding! : grinning:: grinning:

If you have any questions, please contact us.

that's all. Thank you.

Recommended Posts

How to use environment variables in RubyOnRails
How to use Java variables
[Rails] How to use PostgreSQL in Vagrant environment
How to use Lombok in Spring
[Processing × Java] How to use variables
How to use InjectorHolder in OpenAM
How to name variables in Java
Multilingual Locale in Java How to use Locale
How to use custom helpers in rails
[Docker] Use environment variables in Nginx conf
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
Understand in 5 minutes !! How to use Docker
How to use credentials.yml.enc introduced in Rails 5.2
How to use ExpandableListView in Android Studio
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Rails] How to use select boxes in Ransack
How to use "sign_in" in integration test (RSpec)
How to install Titan2D (v4.2.0) in virtual environment
How to use JQuery in js.erb of Rails6
How to embed JavaScript variables in HTML with Thymeleaf
[Java] How to use Map
How to create a Java environment in just 3 seconds
[Ruby] How to use standard output in conditional branching
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
How to use Z3 library in Scala with Eclipse
[Rails] How to use enum
[Swift] How to use UserDefaults
How to use java class
Understand how to use Swift's JSON Decoder in 3 minutes
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args