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

14

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
Sedgewake answered 1/9, 2011 at 19:43 Comment(2)
bundle exec rake assets:precompile seems to fix it, but why doesn't it just work?Sedgewake
I have the same problem in production, even after run bundle exec rake assets:precompileRheinland
S
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.

Sedgewake answered 2/9, 2011 at 1:18 Comment(9)
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? ThanksLeopold
You might want to try what this article says: devcenter.heroku.com/articles/rails31_heroku_cedar (I haven't tried it myself yet)Sedgewake
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.Cedeno
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.Ligament
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?Sanguinolent
Thank you so much it took me two days to find the cause and solution to my issue!Bullwhip
@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 <link>/<script>Africa
Also can use render :layout => false in your action :DTisbee
I could not agree more with @DavidTuite comment. Here's the solution that worked for me: #7426429Turbid
M
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

Margitmargo answered 10/9, 2011 at 1:43 Comment(9)
I can't believe how hard it was to find out how to do this.Lepley
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." guides.rubyonrails.org/asset_pipeline.html#live-compilationDisgruntle
rake -T or bundle exec rake -T is your friend.Kendallkendell
Why use bundle exec rake assets:precompile over rake assets:precompile?Lacrimatory
@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 ....Margitmargo
I found I had to uncomment this Bundler.require(:default, :assets, Rails.env) in config/application.rb for this to work alsoAugust
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=productionWonderstricken
And don't forget to restart your server after precompiling the assets (nginx for example).Dominic
you might have to run this as a root user.Paratyphoid
C
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 \;
Chromosphere answered 18/4, 2012 at 20:32 Comment(2)
If you use media queries and different css files for different resolutions, this is the best way to go.Demetrademetre
I have to include all my css files in array? What if I have 40 css files?Cornwall
S
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'
Selection answered 21/11, 2011 at 17:15 Comment(0)
E
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.

Ethylethylate answered 29/12, 2012 at 19:8 Comment(0)
K
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
Krefetz answered 16/12, 2011 at 13:40 Comment(2)
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.Rochellerochemont
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..Amplitude
C
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)
Countryandwestern answered 11/1, 2013 at 0:57 Comment(1)
Just want to clarify for anyone coming across this later, the code you need to change above is located in config/application.rbGiorgi
M
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

Mesosphere answered 17/11, 2011 at 18:18 Comment(0)
R
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.

Rickets answered 7/12, 2011 at 18:28 Comment(0)
A
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.)

Apery answered 17/9, 2012 at 0:16 Comment(0)
P
1

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

Pratte answered 20/7, 2014 at 16:35 Comment(0)
C
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
Clatter answered 23/4, 2012 at 10:36 Comment(0)
M
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 :)

Milkman answered 7/2, 2013 at 6:47 Comment(0)
H
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

Halter answered 18/5, 2015 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.