breaking into the unknown…

asset not precompiled in rails 3

Leave a comment

Asset Pipeline is introduced as core feature of rails with rails 3.1 . The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB.

Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets. Rails 3.1 is integrated with Sprockets through Action Pack which depends on the sprockets gem, by default.

Since, asset precompilation is now core feature of rails, the asset pipeline is active by default for Development Environment. Any files within app/assets folder will be precompiled by the asset pipeline. You can turn it off by adding below line to your configuration file for production, staging, development or whatever it is.

config.assets.enabled = false # By default it is true for Development Environment and false for other
This will turn off asset pipeline, so now your assets is not precompiled. But somehow, your rails application is configured to work with compiled assets only, so throwing this error. So , the solution is to remove above line from your configuration file or precompile the assets yourself
SOLUTIONS 1: Not recommended
enable the asset pipeline for the environment causing the problem, say in production.rb make it .
config.assets.enabled = true
This is not recommended in production. Some discussion available here on stackoverflow. In short, if you set it true in production, this will happen when say image1.png is requested by browser first time, the request will be passed to  Sprockets which will compiled and cached in whatever Rails is using for cache (usually the filesystem) and served back.On subsequent requests Sprockets receives the request and has to look up the fingerprinted filename, check that the file (image) or files(css and js) that make up the asset were not modified, and then if there is a cached version serve that. doing all this involve a lot of overhead and will slow down your application
SOLUTION 2: Manually Precompiling the assetsThis is recommended solution for Production. When assets are precompiled and compile is off, assets are compiled and fingerprinted to the public/assets. Sprockets returns a mapping table of the plain to fingerprinted filenames to Rails, and Rails writes this to the filesystem. The manifests.yml file is loaded into Memory by Rails at startup and cached for use by the asset helper methods.This makes the generation of pages with correct fingerprinted assets very fast, and the serving of the files themselves are web-server-from-the-filesystem fast. Both dramatically faster than live compiling.Set following configuration in production.rb environment file.

config.assets.enabled = false # live compilation is disabled
config.assets.digest = true # The asset will be compiled with fingerprinting, this is must as Sprockets look to fingerprint to check file is changed or not
config.assets.compress = true # will compress css and js file
config.assets.precompile += %w( search.js ) # search.js is say manifest file i,e it include list of other not use this line if you do not have any left out manifest, search.js in my case. I have just mentioned it here as a example to precompile asset put in manifest different from the default one i,e application.css, application.js. All file you have included in application.css and application.js will be precompiled by default. If something included in other file, say search.js , it can be included as here.
Now Login to your production server and compile the asset manually with below command.

bundle exec rake assets:precompile RAILS_ENV=production

If you do not have write access to your production file system, you can call this task locally and then deploy the compiled assets.

bundle exec rake assets:precompile RAILS_ENV=development

Compiled assets are written to the location specified in config.assets.prefix. By default, this is the public/assets directory.Also note that, a no of default for asset pipeline work only for production. for example , Fingerprinting is enabled by default for production and disabled for all other environments. You must enable it in development.rb file as below, if you want to precompile the assets on development machine and then deploy the compiled assets  to production
config.assets.digest = true # if you precompile your asset directly in environment other then production, by above command you need to set this option to true, otherwise, you will again get the error asset not precompiled

Author: arunyadav4u

over 7 years experience in web development with Ruby on Rails.Involved in all stage of development lifecycle : requirement gathering, planing, coding, deployment & Knowledge transfer. I can adept to any situation, mixup very easily with people & can be a great friend.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s