Posted over 4 years ago. Visible to the public.

Asset Pipeline Basics

The Rails asset pipeline improves delivery of application assets (javascripts, stylesheets, images, fonts). Here are some basic facts about its inner workings.

No magic

Manifests are the handle on your assets:

Copy
app/assets/stylesheets/application.css # use via: stylesheet_link_tag 'application'

The asset pipeline only considers files you explicitly require within your manifest files. The most common directives used in manifests are require some/file and require_tree some/directory. Paths may be relative to the current directory or any of the asset search paths (see below).

Assets search path

By default, the following directories are considered, in the given order:

Copy
app/assets # for application stuff lib/assets # for stuff that does not seem to fit into app/assets vendor/assets # for external stuff

Grouping assets

Asset pipeline files may be grouped using an index file and then be used by simply requiring the directory they live in.

Index files are just sub-manifests. The need the corresponding file ending (e.g. .js for Javascript files), because "Sprockets assumes you are requiring a .js file when done from within a .js file".

Example

If there is an index file app/assets/stylesheets/blocks/index.css, you may simply require blocks in your application manifest. The index file is in charge of requiring the relevant files below its directory.

Copy
app assets stylesheets application.css blocks index.css foo.css.sass bar.css.sass
Copy
# app/assets/stylesheets/blocks/index.css *= require foo *= require bar
Copy
# app/assets/stylesheets/application.css *= require blocks

Debugging

One step in debugging the asset pipeline is to check the precompilation results. You can do this locally using the following commands:

Copy
rake assets:precompile # precompiles to Rails.root/public/assets rake assets:clobber # deletes the public/assets directory

After precompilation, you can check the generated files in public/assets for correct URLs in CSS files etc.

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
over 2 years ago
by Dominik Schöler
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more