rails 3.1.0 ActionView::Template::Error (application.css isn't precompiled)

Ruby on-RailsAsset Pipeline

Ruby on-Rails Problem Overview


I made a basic rails app with a simple pages controller with an index function and when I load the page I get:

ActionView::Template::Error (application.css isn't precompiled):
    2: <html>
    3: <head>
    4:   <title>Demo</title>
    5:   <%= stylesheet_link_tag    "application" %>
    6:   <%= javascript_include_tag "application" %>
    7:   <%= csrf_meta_tags %>
    8: </head>
  app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb__43625033_88530400'

Gemfile

source 'http://rubygems.org'

gem 'rails', '3.1.0'

# Bundle edge Rails instead:
# gem 'rails',     :git => 'git://github.com/rails/rails.git'

gem 'sqlite3'

gem 'execjs'
gem 'therubyracer'

# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails', "  ~> 3.1.0"
  gem 'coffee-rails', "~> 3.1.0"
  gem 'uglifier'
end

gem 'jquery-rails'

# Use unicorn as the web server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

group :test do
  # Pretty printed test output
  gem 'turn', :require => false
end

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

# config/environments/production.rb
...
config.assets.compile = 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.

Solution 2 - Ruby on-Rails

You will get better performance in production if you set config.assets.compile to false in production.rb and precompile your assets. You can precompile with this rake task:

bundle exec rake assets:precompile

If you are using Capistrano, version 2.8.0 has a recipe to handle this at deploy time. For more info, see the "In Production" section of the Asset Pipeline Guide: http://guides.rubyonrails.org/asset_pipeline.html

Solution 3 - Ruby on-Rails

OK - I had the same problem. I didn't want to use "config.assets.compile = true" - I had to add all of my .css files to the list in config/environments/production.rb:

config.assets.precompile += %w( carts.css )

Then I had to create (and later delete) tmp/restart.txt

I consistently used the stylesheet_link_tag helper, so I found all the extra css files I needed to add with:

find . \( -type f -o -type l \) -exec grep stylesheet_link_tag {} /dev/null \;

Solution 4 - Ruby on-Rails

A quick fix for capistrano user is to put this line to Capfile

# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'

Solution 5 - Ruby on-Rails

For all those who are reading this but do not have problem with application.css and instead with their custom CSS classes e.g. admin.css, base.css etc.

Solution is to use as mentioned

bundle exec rake assets:precompile

And in stylesheets references just reference application.css

<%= stylesheet_link_tag    "application", :media => "all" %>

Since assets pipeline will precompile all of your stylesheets in application.css. This also happens in development so using any other references is wrong when using assets pipeline.

Solution 6 - Ruby on-Rails

I was having the exact same error in my development environment. In the end all I needed to do in order to fix it was to add:

config.assets.manifest = Rails.root.join("public/assets")

to my config/environments/development.rb file and it fixed it. My final config in development related to assets looks like:

config.assets.compress = false  
config.assets.precompile += %w[bootstrap-alerts.js] #Lots of other space separated files
config.assets.compile = false
config.assets.digest = true
config.assets.manifest = Rails.root.join("public/assets")
config.assets.debug = true

Solution 7 - Ruby on-Rails

I also had this issue, where trying to run in production without precompiling it would still throw not-precompiled errors. I had to change which line was commented application.rb:

  # If you precompile assets before deploying to production, use this line
  # Bundler.require(*Rails.groups(:assets => %w(development test)))
  # If you want your assets lazily compiled in production, use this line
  Bundler.require(:default, :assets, Rails.env)

Solution 8 - Ruby on-Rails

Here's the quick fix:

If you're using capistrano do this add this to your deploy.rb:

after 'deploy:update_code' do
  run "cd #{release_path}; RAILS_ENV=production rake assets:precompile"
end

cap deploy

Solution 9 - Ruby on-Rails

I ran into this error message today and wanted to post the resolution to my particular my case. It turns out that my problem was that one of my css files was missing a closing brace and this was causing the file to not be compiled. It may be harder to notice this if you have an automated process that sets everything up (including the asset precompile) for your production environment.

Solution 10 - Ruby on-Rails

After all else failed...

My solution was to change the layout file from

= stylesheet_link_tag "reset-min", 'application'

to

= stylesheet_link_tag 'application'

And it worked! (You can put the reset file inside the manifest.)

Solution 11 - Ruby on-Rails

Just another way to fix this up on Heroku: Make sure your Rakefile is committed and pushed.

Solution 12 - Ruby on-Rails

On heroku server (readonly filesystem), If you want runtime compilation of css (its not recommended but you can do it), make sure you have done settings like below -

# inside config/application.rb
config.assets.enabled = true
config.assets.prefix = Rails.root.join('tmp/assets').to_s

# If you are using sass then keep gem outside of asset group
 gem 'sass-rails',   '3.1.4'

# inside config/environments/production.rb
config.assets.compile = true

Solution 13 - Ruby on-Rails

if you think you followed everything good but still unlucky, just make sure you/capistrano run touch tmp/restart.txt or equivalent at the end. I was in the unlucky list but now :)

Solution 14 - Ruby on-Rails

You probably have a syntax error in the css you are using.

Run this command

$ bundle exec rake assets:precompile RAILS_ENV=development --trace

It will give the exception, fixed that and you are all done.

Thanks

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris MuenchView Question on Stackoverflow
Solution 1 - Ruby on-RailsChris MuenchView Answer on Stackoverflow
Solution 2 - Ruby on-RailsrichardsunView Answer on Stackoverflow
Solution 3 - Ruby on-RailsDon LawView Answer on Stackoverflow
Solution 4 - Ruby on-Railschifung7View Answer on Stackoverflow
Solution 5 - Ruby on-RailsHaris KrajinaView Answer on Stackoverflow
Solution 6 - Ruby on-RailsdarchitectView Answer on Stackoverflow
Solution 7 - Ruby on-RailsmathdancerView Answer on Stackoverflow
Solution 8 - Ruby on-RailsfivetwentysixView Answer on Stackoverflow
Solution 9 - Ruby on-RailsSnapShotView Answer on Stackoverflow
Solution 10 - Ruby on-RailsVictor PudeyevView Answer on Stackoverflow
Solution 11 - Ruby on-RailsphillbakerView Answer on Stackoverflow
Solution 12 - Ruby on-RailsSandip RansingView Answer on Stackoverflow
Solution 13 - Ruby on-RailsMuntasimView Answer on Stackoverflow
Solution 14 - Ruby on-RailsShoaib MalikView Answer on Stackoverflow