LoginSignup
2
2

More than 5 years have passed since last update.

settingslogicで環境によって異なる定数を扱う

Last updated at Posted at 2012-09-06

settingslogicというgemで環境ごとに異なる定数が使える。

設定方法

Gemfileに記述して$ bundle install

Gemfile
gem "settingslogic"

設定ファイル用のモデル作成

app/model/settings.rb
class Settings < Settingslogic
  source "#{Rails.root}/config/application.yml"
  namespace Rails.env
end

続いて、config/application.ymlを作成
 例:locationという定数を設定

config/application.yml
development:
  location: "Saitama"

production:
  location: "Tokyo"

これでOK。

使い方

Settings.設定項目名で設定した定数が参照可能になる
上記の例だと
 開発環境ならSettings.locationが"Saitama"、
 本番環境だと"Tokyo"として読み込まれる

参考:http://d.hatena.ne.jp/ramtiga/20110418/p1
   http://d.hatena.ne.jp/seiunsky/20100912/1284299997

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