[Rails 3.1.0] ActionView::Template::Errror (application.css isn’t precompiled)

Ruby on Rails 3.1.0 にて、アプリケーションを作成してdevelop環境は作らずに、Heroku上のstaging環境でいきなり動かしたら以下のようなエラーが発生しました。

ActionView::Template::Errror (application.css isn't precompiled)

対応策としては、config.assets.compile を true にすればOKです。

■ config/environments/staging.rb

# Don't fallback to assets pipeline if a precompiled asset is missed
# config.assets.compile = false
config.assets.compile = true

以下、stackoverflowに書かれていた説明と自分で翻訳したもの。

By default Rails assumes that you have your files precompiled in the production environment, if you want use live compiling (compile your assets during runtime) in production you must set the config.assets.compile to true. You can use this option to fallback to Sprockets when you are using precompiled assets but there are any missing precompiled files. If config.assets.compile option is set to false and there are missing precompiled files you will get an “AssetNoPrecompiledError” indicating the name of the missing file.

デフォルトの設定によって、RailsはProduction環境でプリコンパイル済みのファイルがあることを想定しているので、もしProduction環境でライブコンパイル(runtime中にアセットをコンパイルすること)を使いたいなら、config.assets.compile を true にしなければならない。プリコンパイル済みファイルが全くないが、プリコンパイル済みのアセットを使おうとしているとき、Sprockets *1 の代替策としてこのオプションを使うことができる。config.assets.compile のオプションが false に設定されていて、プリコンパイル済みのファイルがない場合、存在しないファイル名を指して“AssetNoPrecompiledError”が発生するでしょう。

*1 Sprockets: Rubyで書かれたJavaScriptプリプロセッサで、複数のJavaScriptソースファイルを1つにまとめるのが主な機能

【参考】

rails 3.1.0 ActionView::Template::Errror (application.css isn’t precompiled) – Stack Overflow はてなブックマーク - rails 3.1.0 ActionView::Template::Errror (application.css isn't precompiled) - Stack Overflow

Asset Pipelineを備えたRails 3.1のリリースに向けて、その根幹を担うSprocketsをちょっと触っておくことにした。 – Sooey はてなブックマーク - Asset Pipelineを備えたRails 3.1のリリースに向けて、その根幹を担うSprocketsをちょっと触っておくことにした。 - Sooey

Ruby on Rails Guides: Asset Pipeline はてなブックマーク - Ruby on Rails Guides: Asset Pipeline