Railsアプリケーションの環境変数の設定方法は、Rails標準のconfig_for、global gem、config gemの3つが主流です。
今回はそれぞれのケースでの環境変数の設定方法について紹介します。
目次
config_forを利用する方法
config_forとはRails::Applicationに定義されているRails標準のメソッドです。config_forを利用することで設定ファイルの管理ができます。
config_for
はRails標準の機能ですので、後述するglobal gemやconfig gemのような初期設定は不要です。
設定ファイルの作成方法
config_for
はデフォルトでconfigディレクトリに配置されているyamlファイルを参照します。
config_for
の設定ファイルでは環境名をキーとし、各環境で利用したい値を変数に設定します。
config_for
の設定ファイルの具体例は以下の通りです。
config/hosts.yml
test:
web: localhost
api: api.localhost
port: 80
development:
web: localhost
api: api.localhost
port: 80
production:
web: myhost.com
api: api.myhost.com
port: 80
yamlのインジェクト(<<)・アンカー(&)・エイリアス(*)を利用すると重複する箇所をまとめられます。つまり、上記の設定ファイルは以下のように書き換えられます。
config/hosts.yml
default: &default
port: 80
test:
<<: *default
web: localhost
api: api.localhost
development:
<<: *default
web: localhost
api: api.localhost
production:
<<: *default
web: myhost.com
api: api.myhost.com
config_forを利用した場合の変数の参照方法
config_for
を利用する場合、『Rails.application.config_for(:ファイル名)[キー名]』で変数を参照します。
例えば以下のような設定ファイルを作成したとします。
config/hosts.yml
default: &default
port: 80
test:
<<: *default
web: localhost
api: api.localhost
development:
<<: *default
web: localhost
api: api.localhost
production:
<<: *default
web: myhost.com
api: api.myhost.com
上記のファイルで設定されている値は以下のように参照できます。
### test環境
$ rails c -e test
> Rails.application.config_for(:hosts)["web"]
=> "localhost"
> Rails.application.config_for(:hosts)["api"]
=> "api.localhost"
> Rails.application.config_for(:hosts)["port"]
=> 80
### development環境
$ rails c
> Rails.application.config_for(:hosts)["web"]
=> "localhost"
> Rails.application.config_for(:hosts)["api"]
=> "api.localhost"
> Rails.application.config_for(:hosts)["port"]
=> 80
### production環境
$ rails c -e production
> Rails.application.config_for(:hosts)["web"]
=> "myhost.com"
> Rails.application.config_for(:hosts)["api"]
=> "api.myhost.com"
> Rails.application.config_for(:hosts)["port"]
=> 80
Globalを利用する方法
global gemはグローバル変数を定義するgemです。
以下ではglobalの利用方法について紹介します。利用するglobal gemのバージョンは2.0.0です。
Gemのインストール
globalをインストールします。
Gemfile
gem 'global'
イニシャライザの設定
Railsアプリケーションでglobalを利用する場合、globalのイニシャライザはconfig/initializers/global.rb
に記述します。イニシャライザの設定項目の詳細はREADMEのConfigurationをご確認ください。
globalがデフォルトで参照するconfig/global
ディレクトリに設定ファイルを配置する場合のイニシャライザは以下の通りです。
config/initializers/global.rb
Global.configure do |config|
config.backend :filesystem
end
設定ファイルの作成方法
globalはデフォルトでconfig/globalに配置されているyamlファイルを参照します。
globalの設定ファイルでは環境名をキーとし、各環境で利用したい値を変数に設定します。
globalの設定ファイルの具体例は以下の通りです。
config/global/hosts.yml
test:
web: localhost
api: api.localhost
port: 80
development:
web: localhost
api: api.localhost
port: 80
production:
web: myhost.com
api: api.myhost.com
port: 80
globalではdefaultというキー名を利用することで変数のデフォルト値を設定できます。デフォルト値は上書きしない限り任意の環境で参照可能です。つまり、上記の設定ファイルはdefault
を利用して以下のように書き換えられます。
default:
web: localhost
api: api.localhost
port: 80
production:
web: myhost.com
api: api.myhost.com
Globalを利用した場合の変数の参照方法
globalを利用する場合、『Global.[ファイル名].[キー名]』で変数を参照します。
例えば以下のような設定ファイルを作成したとします。
config/hosts.yml
default:
web: localhost
api: api.localhost
port: 80
production:
web: myhost.com
api: api.myhost.com
上記のファイルで設定されている値は以下のように参照できます。
### test環境
$ rails c -e test
> Global.hosts.web
=> "localhost"
> Global.hosts.api
=> "api.localhost"
> Global.hosts.port
=> 80
### development環境
$ rails c
> Global.hosts.web
=> "localhost"
> Global.hosts.api
=> "api.localhost"
> Global.hosts.port
=> 80
### production環境
$ rails c -e production
> Global.hosts.web
=> "myhost.com"
> Global.hosts.api
=> "api.myhost.com"
> Global.hosts.port
=> 80
Configを利用する方法
config gemは各環境の設定ファイルを作成するgemです。
以下ではconfigの利用方法について紹介します。利用するconfig gemのバージョンは3.1.0です。
Gemのインストール
configをインストールします。
Gemfile
gem 'config'
config:installの実行
config:install
を実行し、configに必要なファイルを作成します。
$ rails g config:install
Running via Spring preloader in process 1341
create config/initializers/config.rb
create config/settings.yml
create config/settings.local.yml
create config/settings
create config/settings/development.yml
create config/settings/production.yml
create config/settings/test.yml
append .gitignore
設定ファイルの作成方法
configはconfig/settings.ymlとconfig/settings/[環境名].ymlのファイルを参照します。
config/settings.ymlは変数のデフォルト値を設定するファイル、config/settings/[環境名].ymlは各環境で利用したい値を設定するファイルです。
configの設定ファイルの具体例は以下の通りです。
config/settings/test.yml
hosts:
web: localhost
api: api.localhost
port: 80
config/settings/development.yml
hosts:
web: localhost
api: api.localhost
port: 80
config/settings/production.yml
hosts:
web: myhost.com
api: api.myhost.com
port: 80
上記の設定ファイルはconfig/settings.ymlを利用して以下のように書き換えられます。
config/settings.yml
hosts:
web: localhost
api: api.localhost
port: 80
config/settings/production.yml
hosts:
web: myhost.com
api: api.myhost.com
Configを利用した場合の変数の参照方法
configを利用する場合、『Settings.[キー名]』で変数を参照します。
例えば以下のような設定ファイルを作成したとします。
config/settings.yml
hosts:
web: localhost
api: api.localhost
port: 80
config/settings/production.yml
hosts:
web: myhost.com
api: api.myhost.com
上記のファイルで設定されている値は以下のように参照できます。
### test環境
$ rails c -e test
> Settings.hosts.web
=> "localhost"
> Settings.hosts.api
=> "api.localhost"
> Settings.hosts.port
=> 80
### development環境
$ rails c -e
> Settings.hosts.web
=> "localhost"
> Settings.hosts.api
=> "api.localhost"
> Settings.hosts.port
=> 80
### production環境
$ rails c -e production
> Settings.hosts.web
=> "myhost.com"
> Settings.hosts.api
=> "api.myhost.com"
> Settings.hosts.port
=> 80
config_for/global/configの比較まとめ
環境の区別方法 | デフォルトの参照先 | デフォルト値の設定可否 | 変数の参照方法 | |
---|---|---|---|---|
config_for | キーに環境名を定義 | config/ | 否 | Rails.application.config_for(:ファイル名)[キー名] |
global | キーに環境名を定義 | config/global | 可 | Global.[ファイル名].[キー名] |
config | 環境ごとの設定ファイルを用意 | config/settings | 可 | Settings.[キー名] |
さいごに
Twitter(@nishina555)やってます。フォローしてもらえるとうれしいです!