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

Tadashi Shigeoka ·  Sat, November 5, 2011

In Ruby on Rails 3.1.0, I created an application without setting up a development environment and tried to run it directly in the staging environment on Heroku, which resulted in the following error:

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

The solution is to set config.assets.compile to true.

■ config/environments/staging.rb

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

Below is the explanation written on StackOverflow and my translation:

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つにまとめるのが主な機能

Translation: “By default, Rails assumes that there are precompiled files in the Production environment, so if you want to use live compiling (compiling assets during runtime) in Production, you must set config.assets.compile to true. You can use this option as a fallback to Sprockets when you are using precompiled assets but there are no precompiled files at all. If the config.assets.compile option is set to false and there are no precompiled files, an ‘AssetNoPrecompiledError’ will occur pointing to the missing file name.

*1 Sprockets: A JavaScript preprocessor written in Ruby whose main function is to combine multiple JavaScript source files into one”

【References】

rails 3.1.0 ActionView::Template::Errror (application.css isn’t precompiled) - Stack Overflow

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

Ruby on Rails Guides: Asset Pipeline

That’s all from the Gemba.