【Rails】定数は、config gemで管理しましょう
環境
$ rails -v
Rails 6.0.3.1
$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]
まずは、Gemのインストール
Gemfile
gem 'config'
$ bundle install --path vendor/bundle
configの初期設定を行う
$ rails -v
Rails 6.0.3.1
$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]
Gemfile
gem 'config'
$ bundle install --path vendor/bundle
configの初期設定を行う
configの初期設定のために関連ファイルをインストールする
$ bundle exec rails g config:install
定数定義と、使い方
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"
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"
Author And Source
この問題について(【Rails】定数は、config gemで管理しましょう), 我々は、より多くの情報をここで見つけました https://qiita.com/beanzou/items/87fb89f73e1b9e6490e1著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .