LoginSignup
36
36

More than 5 years have passed since last update.

figaroでrailsで環境ごとの定数管理してherokuに設定するまで

Last updated at Posted at 2014-06-04

railsで環境ごと(development,staging,production)の定数をどこで持てばいいんだろうと考えて、config/environments/developmentあたりに書いてRails.application.config.HOGEとかでアクセスするとか、どう考えてもみんなやってないだろうなと思って、他にもdotenvとかもあるらしいけど今回は"figaro"というgemを使ってみることに。

bundle installして、

gem 'figaro'

インストール。
以下だとなぜか怒られた

rails g figaro install
# > Could not find generator figaro

なので、

rails generate figaro:install

で、config/application.ymlと.gitignoreに/config/application.ymlが追加される。
先輩にならってgit上で管理・共有したい設定は、application.yml.exampleとか作ってそちらはgitのリポジトリで管理。

application.yml
KEY: '1234567'

development:
  CLIENT_URL: 'http://localhost:8080'

staging:
  CLIENT_URL: 'http://example-stg.com'

production:
  CLIENT_URL: 'http://example.com'

こんな感じで設定したものを、

Figaro.env.CLIENT_URL

こうやって参照できる。
Figaro.env is a convenience that acts as a proxy to ENV.とのことなので以下でいいのか。

ENV["CLIENT_URL"]

herokuにpushする前に設定を追加する。

rake figaro:heroku[appname-stg] RAILS_ENV=staging
rake figaro:heroku[appname] RAILS_ENV=production

便利。

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