17
11

More than 5 years have passed since last update.

config.x を利用して、Rails.configurationにユーザ定義値をセットする

Posted at

Rails Guide http://guides.rubyonrails.org/configuring.html#custom-configuration によると、Rails.configuration(=config/application.rb中で参照されているconfig)にユーザ定義のカスタム値を設定できるとあるのだが、

in config/application.rb:

config.hello = 'world'

というように、config直下に指定するのはできるものの、

config.hello.world = 'oyster'

というように階層化したりするのはエラーになる。

Hashを渡すなどすれば使えないことはないので、それほど困ることはないが、

Realize that this is specifically the x namespace and you cannot rename it! I originally got tripped up by it because I thought you could use whatever you want as per the documentation was not super specific on the x portion. But as per the 4.2 release notes it clearly states that they "Introduced the x namespace for defining custom configuration options".
Rails 4 Custom Configurations : http://www.richardhsu.me/posts/2015/04/02/rails-4-custom-configurations.html

という記事を発見。これによるとRails4.2では、

config.x

というのが公式のカスタムネームスペースになっていて、その下に定義しろ、ということらしい。こちらだと、ピリオドで階層化した設定も問題なくできた。

config.x.hello.world = 'oyster'
Rails.configuration.x.hello.world # -> 'oyster'
17
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
11