LoginSignup
14
14

More than 5 years have passed since last update.

Heroku本番環境にて、Assetが正常に反映されない。

Posted at

開発環境

Ruby  2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]
Rails 4.2.10
Heroku

起こった問題

Ruby on Railsで作成したアプリケーションを、Herokuでデプロイしました。その際、development環境では正しく表示されていたビューが、Herokuデプロイ後のproduction環境では以下のように崩れてしまいます。アセットパイプラインでの処理に問題があるようです。

スクリーンショット

development環境では正しく表示されていたビューが

Image from Gyazo

production環境では崩れている

Image from Gyazo

わかりにくいですが、サービスロゴが表示されない、フォントカラーやボーダーカラーが指定したものと異なる。などなど

原因

Herokuではアセットパイプラインに関して必ず設定が必要です。以下に手順を記載します。

1. config/application.rbに下記コードを追記

config.assets.initialize_on_precompile = false

2. ターミナルにて下記コマンド実行

$ heroku run bundle exec rake assets:precompile RAILS_ENV=production -a [appname]

説明

heroku run [コマンド] -a [appname]
Herokuサーバーにてコマンドを実行するために必要なコマンド。Herokuに登録されているアプリ名をオプションで指定する必要あり。
bundle exec rake assets:precompile RAILS_ENV=production
アセットのプリコンパイルを手動で実行するためのコマンド。production環境を指定し実行している。
```

※以下にHerokuドキュメントの抜粋を記載します。

Troubleshooting
Failures in the assets:precompile task
In Rails 3.x, you can prevent initializing your application and connecting to the database by ensuring that the following line is in your config/application.rb:
config.assets.initialize_on_precompile = false
Do not forget to commit to git after changing this setting.
Before you can compile your assets on Heroku, you’ll need to be able to compile them locally, run this command to debug your assets:
$ RAILS_ENV=production bundle exec rake assets:precompile
This should complete with no errors. Do NOT check in the assets into git after running this command.

参考

RailsGuidesアセットパイプライン
Rails Asset Pipeline on Heroku
RubyStudioアセットパイプライン
Rails4 asset pipeline関連設定まとめ(Heroku対応込)
Rails初学者がつまずきやすい「アセットパイプライン」

最後に

どなたかの助けになれば幸いです。間違っている点、アドバイスなどあればコメントいただければと思います。

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