LoginSignup
18
18

More than 5 years have passed since last update.

Herokuへのデプロイでつまづいたことまとめ

Last updated at Posted at 2015-07-27

環境

Rails 4.2.2

そんなことより

ここを読もうぜ(全部これで解決する)
https://devcenter.heroku.com/articles/getting-started-with-rails4

つまづいたことメモ

DBはpostgresqlを使うこと

#gem 'sqlite3'
gem 'pg'

場合によってはbundle updateでGemfile.lockを更新する必要があるかもしれない。

また、config/database.yml を以下のように変更する。

config/database.yml
default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000
  encoding: unicode

development:
  <<: *default
  database: アプリ名_development

test:
  <<: *default
  database: アプリ名_test

production:
  <<: *default
  database: アプリ名_production

ローカルでのpostgresqlの導入までカバーすると大変なのでその辺は別途Google先生にお頼みしたい次第です。
自分の場合はローカルですでにsqlite3上でmigrateをしてしまっていたので、$bundle exec rake db:migrate:resetで初回からmigrateやりなおしするのが必要でした。

参考: http://qiita.com/mm36/items/f1d6a1bdc1023ebe1c62

rails_12factorのgemを導入

公式さんより
https://devcenter.heroku.com/articles/getting-started-with-rails4#heroku-gems

Herokuの統合環境はRails4で削除されたRailsプラグインに依存していました。静的なassets servingとloggingといった機能をHeroku上で有効にするために、あなたのGemfilerails_12factorを追加してください。

(注:意訳です)
ということなので素直にGemfileに追加します。

gem 'rails_12factor', group: :production

それから$ bundle installとのこと。

deployできたと思ったらInternal Server Error吐いた

Missing secret_token and secret_key_base for 'production' environment, set these values in config/secrets.yml

原因はconfig/secrets.ymlを.gitignoreで無視してリポジトリに含めていなかったこと。
secrets.yml の中身にはセッションで用いる暗号化キー(?)やAPIキーなど色々定義できる様子。productionのキーはcommitしたくないと思うところだが、環境変数を参照するようになっているので大丈夫な様子。

環境変数はHerokuさんのほうがよしなにやってくださっている様子。
https://devcenter.heroku.com/changelog-items/426
以下超アバウトな意訳(2番)

configの SECRET_KEY_BASE 変数はRails 4.1アプリのlifetimeにて生成されます。新しいRails 4.1.0 RC1 アプリはこれを設定しますが、あなたのconfig/secrets.ymlは次のように書いてください。

config/secrets.yml
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

SECRET_KEY_BASE には新しい値をセットすることができます。

$ heroku config:set SECRET_KEY_BASE=`ruby -rsecurerandom -e "puts SecureRandom.hex(64)"`

(lifetime と to roll your credentials ってどう訳せばいいんだろう...)

参考:
- http://qiita.com/kanpe777/items/cb11dc88ced544d10bd5
- http://easyramble.com/rails-secret-key-base-env.html
- http://t4traw.github.io/20141215/study-rails-secrets-yml.html

modelが無いっぽくて We're sorry, but something went wrong. と言われちゃう

$ heroku logsを見たところ、

ActionView::Template::Error (PG::UndefinedTable: ERROR:  relation "cards" does not exist

肝心のmigrateを忘れているようでした。

$ heroku run rake db:migrate

おまけ、assets:precompileでトラブル

remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        I, [2015-07-27T22:48:18.024011 #1007]  INFO -- : Writing /tmp/build_2bfe1d5f5d5d24c9965ede0f9a198f5f/public/assets/application-ffb31ff9fed8e5d57837a7022f8809d296f3b8f0de91fc2b6d8d29b92f9b55dc.js
remote:        rake aborted!
remote:        Sprockets::ArgumentError: require_tree argument must be a directory
(中略)
remote:  !     Precompiling assets failed.

と言われてしまった。
何か変なことしたかと思っていましたが、中にcssファイルを1件も入れていない空のディレクトリをapplication.cssで読み込んでいたのが原因でした。
themes以下が空っぽだったので、以下のように指定してあるのを削除しました。

app/assets/stylesheets/application.css
 *= require_tree ./themes

まぁこれは個人的な問題だった...。

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