Two weeks ago, I was introduced to the Asset Pipeline. I didn’t really think much of it at first, and part of me even assumed that I’d never really have to see it again. It was a little like going out on a pretty forgettable first date – nothing particularly intriguing about the whole thing, but it somehow leaves you feeling kind of confused and generally rather perplexed.
<script> or a
<link> tag, like so:
<link href="custom.css" rel="stylesheet" type="text/css"/>
Imagine doing that for every single file in your application. Are you in pain yet? Yeah, me too.
“I think empty folder and empty files are two of the pivotal innovations in Rails that has encouraged us to write clean applications since the framework appeared. And I think this is true because when you have a place for everything and everything is in it’s place, things feel nice.”
– David Heinemeier Hansson, RailsConf 2011
Explain me some pipeline
lib/assetsdirectory: for assets for your own libraries’ code.
assets directory are two subdirectories:
Within these folders are manifests, which, in the case of your CSS files, would be included as part of the default application layout in the
app/assets/stylesheets/application.css.scss directory. A manifest sets directives to provide the exact order and list of files that should be concatenated and included in the single CSS file that is delivered to the browser.
So…what exactly is a directive, again?
Directives are nothing more than directions of which files to grab and glob together. In the comments at the top of any manifest, you can find something that looks like
*= require_tree, which is a directory that includes the current directory plus its subdirectories.
The most basic directive is
require, which will concatenate the content of the specified file only once into the final packaged asset (called the “bundle”).
Another common directive is
include, which is similar to
require, except that if it reads the same file more than once, it will insert it into the bundle again.
Other directives like
require_directory are useful in grabbing the current file (such as the manifest file), a specified directory.
Directives are always are processed in the order that they are read in the file, with
require_tree being the one exception to this rule.
Help me help you (or just use a helper method)
The Asset Pipeline has helper methods to make your life a little easier. Two of the most common helper methods are the
Another useful method is
link_to, which can replace the anchor
<a> tags while linking to other parts of your application. This method takes two parameters: the string displayed as the anchor text for the link, and the route:
1 2 3 4
This helper method is pretty awesome because it allows you name a route and then target all of your link locations to your
routes.rb file. This is particuarly great you’re moving your website from say,
www.cats.com to a new and improved dynamic website that follows RESTful conventions, like maybe
www.supercutekittens.com. Imagine having to create absolute URLs for every single link on that website! Ok, don’t panic, you don’t have to.
When you generate a scaffold or a controller(
rails g scaffold or
- Keeping your assets in their proper place will make you a happier and more organized programmer.
- The Asset Pipeline will make your application perform better. Remember that it’s easier (and faster) for your browser to request one large file from the server, rather than a ton of small ones.
- Other things to look into: Fingerprinting, which makes the name of a file dependent purely on its content. When the file contents change, the filename also changes.
- The Asset Pipeline is magical, yet super difficult to learn. You probably won’t understand everything all at once. But hopefully you feel a less confused than this little guy. He’s clearly had too much pipeline for one day:
So, I met the asset pipeline today… pic.twitter.com/gU43HTzmF6— Vaidehi Joshi (@VaidehiJoshi) November 4, 2014