LoginSignup
50
45

More than 5 years have passed since last update.

RailsでアプリをHerokuにあげる時のエラー各種

Last updated at Posted at 2016-01-21

RailsでアプリをHerokuにあげるたびに何かしらのエラーに悩まされてはググってるので、今まで出会ってきたHerokuプッシュ時のエラーについて逐次ここに載せていきます。自分用メモ。

git push heroku master
は簡単なおまじないだけど、素人にはエラーの宝庫なんです。

Railsアップの準備はこことかを参考にしてください。

注意

以下に書いてある+とか-は差分のことで、+の後ろに続く文字列を追加してくださいね!って事です。そのままコピペはしないように。

出会ったエラー達

Failed to install gems via Bundler

本番環境でもSQLite3が設定してあるとこういったエラーが出ます。
rails new で普通に作ると初期状態でSQLite3に設定してあるけれど、Herokuは SQLite3 に対応していないので、あげる際は PostgreSQL(pg) に設定してあげる。

Gemfile
- gem 'sqlite3'

group :development do
+  gem 'sqlite3'
end
group :production do
+  gem 'pg'
end
config/database.yml
production:
+  adapter: pg
+  database: db/production.pg
-  <<: *default
-  database: db/production.sqlite3

こんな感じに本番環境をpgにしてあげれば動くと思います。(自分はこれでokだった)

Precompiling assets failed.

git pushしたときにrake assets:precompileが失敗したとのことなのでこれを追加。

config/application.rb
+ config.assets.initialize_on_precompile = false

NoMethodError: undefined method[]' for nil:NilClass`

rake assets:precomiple中にこんなエラーが起きて毎度お馴染みのrake aborted!って怒られた。調べてみると、StackOverflowさんに答えがあって

application.cssapplication.css.scssにリネームすれば解決した。

WARNING

You have not declared a Ruby version in your Gemfile.

これはGemfileでrubyのバージョンを指定してないと出る忠告。gemfileにrubyのバージョンを追加するだけで消える。

$ ruby -v
ruby 2.2.3p173

バージョンは適宜変えてください。

Gemfile
+ ruby '2.2.3'

No Procfile detected, using the default web server (webrick)

エラーに比べると断然読みやすくて優しい。procfileがないよーって言ってるので追加してあげる。

Procfile
web: bundle exec rails server -p $PORT  # rails 起動($PORTは5000がデフォ)

Internal Server Error

git push heroku masterしてheroku run rake db:migrateしてheroku openしてもこいつが出てくる。。

Internal Server Error(´・ω・)`

Missingsecret_tokenandsecret_key_basefor 'production' environment, set these values inconfig/secrets.yml`

Rails4.1でherokuへのデプロイに失敗(Missing secret_key_base)
このサイトがとても分かり易かった!

.gitignoreにconfig/secrets.ymlがあればコメントアウトする。

.gitignore
# config/secrets.yml

次に環境変数に設定する値を以下のコマンドで出力させる。

$ bundle exec rake secret RAILS_ENV=production
# => xxxxxxxxxxxxxxxxxxxxxxxxxxxx

上記のコマンドで出てきた値(xxxxxx)をheroku上の環境変数(SECRET_KEY_BASE)に格納する。

heroku config:add SECRET_KEY_BASE=xxxxxxx

xxxxxには上記で出力された値をそのままコピペしてください。

50
45
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
50
45