[ruby-on-rails] rails 3.1.0 ActionView::Template::Error (application.css isn't precompiled)

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

This question is related to ruby-on-rails asset-pipeline

The answer is


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)

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


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


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.)


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 :)


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.


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

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


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


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

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

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

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.


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 \;