LoginSignup
67

More than 5 years have passed since last update.

Rails に新たな env を追加する方法

Last updated at Posted at 2015-10-29

Railsはデフォルトで以下のenvがありますが、別のenvを追加したい時もありますよねー
(Rails4.1検証)

  • test
  • development
  • production

例えば Staging というenvでサーバを立ち上げたい

以下のファイルを作成、修正し、RAILS_ENVの環境変数を指定して実行すればOK!!

1 config/database.yml に staging 設定を追記する。

適当に production の設定をコピーするなどして以下の様な設定を入れます。
(mysqlを使っているパターン)

...省略
staging:
  adapter:   mysql2
  encoding:  utf8
  charset:   utf8
  collation: utf8_general_ci
  database:  mydb_staging
  pool:      5
  username:  staging_user
  password:  password
  host:      localhost

2 config/secrets.yml に staging のキーを追加

staging:
  secret_key_base: abcafd..省略

ちなみに、以下のコマンドにて secret key は自動生成できました

$ bundle exec rake secret

3 config/environments/staging.rb を作成

こちらも適当に config/environments/production.rb などをコピーして必要な箇所を変更して作成します。

4 migrate の実行

staging用のDBが無いと思うので、DB作成とマイグレートを実行します。

$ RAILS_ENV=staging bundle exec rake db:create
$ RAILS_ENV=staging bundle exec rake db:migrate

5 必要に応じてその他gemでenv依存のある設定を RAILS_ENV=staging として環境変数を指定して実行します。

特にenvに依存する設定がなければとばしてください。

$ RAILS_ENV=staging bundle exec rake db:seed_fu

6 RAILSアプリサーバの起動

5項までの設定で rails serverstaging の env で立ち上げることができると思います。

$ RAILS_ENV=staging bundle exec rails server

以上です。簡単ですが忘れがちなので備忘録のため。(バージョンが変わるとだめになるかもしれません)

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
67