Engine Yard Developer Center

Rails 3.1 Asset Pipeline Tips

Rails 3.1 introduced the asset pipeline. It is enabled by default. This means that unless you specifically disable the asset pipeline, assets are precompiled for you.

Topics covered on this page:

Using asset path helper methods

With the asset pipeline, static assets are served from a flat file system in the public/assets directory.

Sprockets is integrated with Rails 3.1. Sprockets makes it easier to manage your asset files. Using Sprockets and its asset path helper methods can make ERB more readable.

This section shows two ways to use asset_path helper methods:

To use the Sprockets preprocessor and helper methods to manage assets

  1. Add an .erb extension to your stylesheets.

  2. Use Ruby code in the stylesheets in the same way as in the view files. For example,



    url(<%= asset_path 'rails.png' %>) 

You can also use Sprockets helper methods with the sass-rails gem.

To use the Sprockets preprocessor and helper methods with the sass-rails

  1. Install the sass-rails gem.

  2. Use Ruby code in the stylesheets in the same way as in the view files. For example,



    url(asset-path(‘rails.png’, image))

    Note: Use a hyphen instead of an underscore.


Using the correct CSS or JavaScript files in the staging environment

By default in Rails 3.1, config.assets.digest is set to true for production environments but to false for staging and development environments. If you deploy to your staging environment and your application looks like it has no CSS applied or is using the wrong JavaScript files even though you are precompiling assets (as described above), then you might need to set config.assets.digest to true.

To use the correct CSS and JavaScript files in staging

  • In your environments/staging.rb file, add this line:

    config.assets.digest = true 

    When config.assets.digest is true, when you view a source page for your deployed application, asset names appear with a unique fingerprint. For example:

    <script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js" type="text/javascript"></script> 
    <link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen" rel="stylesheet" type="text/css" />

    instead of:

    <script src="/assets/application.js" type="text/javascript"></script> 
    <link href="/assets/application.css" media="screen" rel="stylesheet" type="text/css" />

The config.assets.digest option turns on “fingerprinting” for each asset. For information about fingerprinting, see the Rails Guides.


Disabling the asset pipeline

There are specific cases where you need to disable precompliation of assets; for example, if your assets are not in the same git repository as your application.

To disable the asset pipeline

  1. Add the line config.assets.enabled = false to the config/application.rb file.
    For example:

    module Listr 
    class Application < Rails::Application
    # Disable the asset pipeline
    config.assets.enabled = false
  2. Save and commit the change.


More information

For more information about the asset pipeline, see the Rails Guides.

If you have feedback or questions about this page, add a comment below. If you need help, submit a ticket with Engine Yard Support.

Was this article helpful?
1 out of 1 found this helpful
Have more questions? Submit a request


Please sign in to leave a comment.

Powered by Zendesk