279
266

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で定数を環境ごとに管理するrails_config(現 config)

Last updated at Posted at 2014-03-05

config って?

開発環境ごとに定数を YAML ファイルで管理できるプラグインのこと

##公式リポジトリ
https://github.com/railsconfig/config

##configをインストール
Gemfile に config を記述

Gemfile
gem 'config'
```

vendor/bundle 以下に config をインストール

```
$ bundle install --path vendor/bundle
$ bundle exec rails g config:install
  create  config/initializers/rails_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
```


gitignoreには自動で下記の内容が追加される

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

使用したい環境に応じて編集するファイルを変更


| YAML(環境) |ファイルの場所 |
|:-----------|:------------|
| 全ての環境のYAML |       config/settings.yml |
| ローカル環境専用のYAML | config/settings.local.yml |
| 開発環境専用のYAML |    config/settings/development.yml |
| テスト環境専用のYAML |  config/settings/test.yml |
| 本番環境専用のYAML |    config/settings/production.yml | 


###記述例

```config/settings.yml
twitter:
  follow_target_name: 'mossmoss'
  text: 'mossmossmossmossmoss'
  url : 'http://mossmossmossmossmoss.com/'
```

###参照例

```
Settings.twitter[:text]
Settings.twitter['text']
Settings[:twitter][:text]
```

 [settingslogic](https://github.com/binarylogic/settingslogic "settingslogic")もいいけど、わかりやすいしそこそこ大きな開発の時に使えそうなrails_configでした。

279
266
3

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
279
266

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?