4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsでconfig(旧rails_ config)というgemを使って定数管理

Posted at

config(旧rails_config)とは

定数を一元管理する便利なgem

Version

gem 'config', '~> 1.2', '>= 1.2.1'

環境

  • ruby 2.2.3p173
  • Rails 4.2.6

Install

  • Gemfile
Gemfile
gem 'config'
  • bundle install
$ bundle install --path vendor/bundle
  • install
$ bundle exec rails g config:install

      create  config/initializers/config.rb # 基本設定
      create  config/settings.yml # 全環境で使用する定数を設定
      create  config/settings.local.yml # ローカル環境でのみ使用する定数を設定
      create  config/settings
      create  config/settings/development.yml # development環境で使用する定数を設定
      create  config/settings/production.yml # production環境で使用する定数を設定
      create  config/settings/test.yml # test環境で使用する定数を設定
      append  .gitignore

.gitignoreに自動で下記が追記される。

.gitignore
config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml

定数をYAMLで定義

例えばdevelopment.ymlに下記のように定義して

config/settings/development.yml
company: 'my compnay'
server:
  url: 'http://myserver'

下記のように呼び出して使用することが可能。

puts Settings.company
puts Settings.server[:url]

[参考]
http://qiita.com/yumiyon/items/32c6afb5e2e5b7ff369e
https://blog.hello-world.jp.net/ruby/2268/

4
4
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
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?