This guide will cover the ideology of the asset pipeline introduced in Rails 3.1.
By referring to this guide you will be able to:
- Understand what the asset pipeline is and what it does
- Properly organize your application assets
- Understand the benefits of the asset pipeline
- Adding a preprocessor to the pipeline
- Package assets with a gem
What Is The Asset Pipeline?
By having this now as a core feature of Rails, all developers can benefit from the power of having their assets pre-processed, compressed and minified by one central gem, Sprockets.
How to Use the Asset Pipeline
In previous versions of Rails, all assets lived under the directory in directories such as , and . With Rails 3.1, the preferred location for these assets is now the directory. Files in this directory will be served by the Sprockets middleware included in the sprockets gem.
This is not to say that assets can (or should) no longer be placed in . They still can be, they will just be served by the application or the web server which is running the application and served just like normal files. You would only use if you wish your files to undergo some pre-processing before they are served.
Assets can be placed inside an application in one of three locations: , or .
is for your own libraries’ code that doesn’t really fit into the scope of the application or those libraries which are shared across applications.
Any subdirectory that exists within these three locations will be added to the search path for Sprockets (visible by calling in a console). When an asset is requested, these paths will be looked through to see if they contain an asset matching the name specified. Once an asset has been found, it’s processed by Sprockets and then served up.
Assets can also come from external sources such as engines. A good example of this is the gem which comes with Rails 3.1 as standard. This gem contains an engine class which inherits from . By doing this, Rails is informed that the directory for this gem may contain assets and the , and directories of this engine are added to the search path of Sprockets.
To serve assets, we can use the same tags that we are generally familiar with:<%= image_tag “rails.png” %>
Providing that assets are enabled within our application ( is set to ), this file will be served by Sprockets unless a file at exists, in which case that file will be served. If there is no file at , Sprockets will look through the available paths until it finds a file that matches the name and then will serve it, first looking in the application’s assets directories and then falling back to the various engines of the application.
Similarly, to include a CSS file we can also still use .<%= stylesheet_link_tag “application” %>
Manifest Files and Directives
For example, in the default Rails application there’s a file which contains the following lines:
//= require jquery
//= require jquery_ujs
//= require_tree .
In JS files, directives begin with . In this case, the file is using the directive twice and the directive once. The directive tells Sprockets that we would like to require a file called that is available somewhere in the search path for Sprockets. By default, this is located inside the directory contained within the gem. An identical event takes place for the require specified here also.
There’s also a default file which contains these lines:
- require_tree .
In this example is used. This will put the CSS contained within the file (if any) at the top of any other CSS in this file unless is specified after another directive.
WIP: Compressed Assets in Rails are served … how?