[RUBY] [Rails] Let's manage constants with config gem

environment

$ rails -v
Rails 6.0.3.1
$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]

First, install Gem

Gemfile


gem 'config'
$ bundle install --path vendor/bundle

Initialize config

Install related files for config initialization

$ bundle exec rails g config:install

Constant definition and usage

config/settings.yml


service: 
  name: 'vdeep' 
  url: 'http://vdeep.net'

authentication_password: "foobarbaz"
$ rails c

> Settings.service.name
=> "vdeep"

> Settings.service[:name]
=> "vdeep"

> Settings[:service][:name]
=> "vdeep"

> Settings.authentication_password
=> "foobarbaz"

Recommended Posts

[Rails] Let's manage constants with config gem
Let's unit test with [rails] Rspec!
[Rails] Manage multiple models using devise gem
[Rails] Manage secret keys etc. with credentials.yml
Let's make an error screen with Rails
[Rails] Organize your code neatly with gem active_decorator
Let's make a search function with Rails (ransack)
[My memo] Let's get along with Pry / DB with Rails
Rails deploy with Docker
[Rails 6] RuntimeError with $ rails s
Handle devise with Rails
[Rails] Test with RSpec
[Rails] Development with MySQL
Let's scrape with Java! !!
Supports multilingualization with Rails!
Double polymorphic with Rails
Why can I use the rails command installed with gem? ??
Get Youtube channel information with Rails app (using Yt gem)
[Rails] Search from multiple columns + conditions with Gem and ransack