281

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
mari
  • 864
  • 3
  • 12
  • 32
Chris Muench
  • 17,444
  • 70
  • 209
  • 362

14 Answers14

313

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.

sethvargo
  • 26,739
  • 10
  • 86
  • 156
Chris Muench
  • 17,444
  • 70
  • 209
  • 362
  • I'm getting the same error, but on Heroku. Issuing a "rake assets:precompile" remotely doesn't work. Do I need anything else like setting any paths or something? Thanks – alvatar Sep 04 '11 at 16:25
  • 3
    You might want to try what this article says: http://devcenter.heroku.com/articles/rails31_heroku_cedar (I haven't tried it myself yet) – Chris Muench Sep 04 '11 at 16:59
  • 7
    Just to be clear, the config.assets.compile option is in config/environments/production.rb (if you are working in production). And if you want to be able to do live/lazy compile in production, you also need to enable the lazy compile in application.rb. – avioing Oct 19 '11 at 22:29
  • 34
    Activating runtime compilation is not the solution, because of the performance hit we take. The solution is to fix the core problem which is preventing asset precompilation from occurring. – David Tuite Oct 27 '11 at 22:50
  • 5
    I precompiled the assets using RAILS_ENV=production bundle exec rake assets:precompile. Why Am I getting this error and needed to set this flag too? – Tony Jan 26 '12 at 20:57
  • Thank you so much it took me two days to find the cause and solution to my issue! – Zakoff Mar 19 '12 at 09:21
  • 2
    @Tony, because you probably ask for something other than application.css/js/image file, and you didn't register it on `application.rb`. Add to/edit `application.rb`: `config.assets.precompile += %w( first.css second.js )`. Now these files will compile as well. Don't add all files if you use them only in sprockets `require`, but only if you include them with ``/` – elado Jan 09 '13 at 10:04
  • Also can use `render :layout => false` in your action :D – Amo Wu May 14 '13 at 15:53
  • 1
    I could not agree more with @DavidTuite comment. Here's the solution that worked for me: http://stackoverflow.com/questions/7425929/rails-3-1-strategy-for-pre-compiling-controller-specific-js-assets – Augustin Riedinger Jun 04 '13 at 16:56
202

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

richardsun
  • 3,245
  • 1
  • 18
  • 22
  • 14
    I can't believe how hard it was to find out how to do this. – derekerdmann Sep 18 '11 at 01:27
  • 3
    This seems like the clearly better option: setting live compile to true "uses more memory, performs more poorly than the default and is not recommended." http://guides.rubyonrails.org/asset_pipeline.html#live-compilation – andrew.rockwell Mar 16 '12 at 19:08
  • `rake -T` or `bundle exec rake -T` is your friend. – rxgx May 15 '12 at 23:17
  • Why use bundle exec rake assets:precompile over rake assets:precompile? – Sebastian Patten Sep 08 '12 at 14:56
  • 2
    @Underworld Bundler is a tool for managing gem dependencies in a ruby app and it is built into Rails 3. If you are using bundler, running `bundle exec rake ...` will ensure that you are loading the right rake and associated dependencies for your app. If you are not using bundler, you would just run `rake ...`. – richardsun Sep 08 '12 at 16:02
  • I found I had to uncomment this `Bundler.require(:default, :assets, Rails.env)` in config/application.rb for this to work also – Martin Capodici Mar 22 '13 at 04:53
  • 3
    If you're going to run the above command, make sure that you're already set your environment to production mode, or prefix the command with RAILS_ENV=production – JESii Apr 29 '13 at 17:59
  • And don't forget to restart your server after precompiling the assets (nginx for example). – Kood Oct 04 '13 at 10:03
  • you might have to run this as a root user. – s2t2 Dec 10 '13 at 03:09
31

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 \;
Don Law
  • 1,329
  • 13
  • 7
30

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

# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
Shawn Chin
  • 84,080
  • 19
  • 162
  • 191
chifung7
  • 2,531
  • 1
  • 19
  • 17
11

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.

Haris Krajina
  • 14,824
  • 12
  • 64
  • 81
8

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
gsandoval
  • 161
  • 2
  • 6
  • This was the only solution that worked for me. Maybe it was a problem with my version of Rails (3.1.3)? Anyway, I could tell my assets had been successfully precompiled to public/assets and their filenames had the same SHAs as listed in manifest.yml. For some reason, Rails was no longer looking for assets in the right place, even though my setup had always worked fine previously. This change fixed that. – antinome Jul 30 '12 at 20:52
  • Thanks for this solution. I was trying to test some things in development mode and I was surprised that it was saying "application.css isn't precompiled" even though I had already precompiled assets and public/assets/application.css was present in the directory. Surprised they don't set config.assets.manifest for you automatically in development like they do in production.. – Tyler Rick Dec 20 '12 at 20:19
5

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)
mathdancer
  • 61
  • 1
  • 2
  • 2
    Just want to clarify for anyone coming across this later, the code you need to change above is located in `config/application.rb` – GMA Jun 03 '13 at 06:53
4

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

fivetwentysix
  • 7,379
  • 9
  • 40
  • 58
3

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.

SnapShot
  • 5,464
  • 5
  • 42
  • 40
1

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

Victor Pudeyev
  • 4,296
  • 6
  • 41
  • 67
1

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

phillbaker
  • 1,518
  • 11
  • 22
0

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
Sandip Ransing
  • 7,583
  • 4
  • 37
  • 48
0

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

Muntasim
  • 6,689
  • 3
  • 46
  • 69
0

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

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
Shoaib Malik
  • 373
  • 4
  • 8