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|asset-pipeline#paths]]
- [[Resolving CSS or JavaScript file problems in a staging environment|asset-pipeline#staging]]
- [[Disabling the asset pipeline|asset-pipeline#disabling]]
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:
- With the Sprockets preprocessor for (regular) CSS
- With the Sprockets preprocessor and the sass-rails gem
-
Add an
.erb
extension to your stylesheets. -
Use Ruby code in the stylesheets in the same way as in the view files. For example,
url('../images/rails.png')
becomes
url(<%= asset_path 'rails.png' %>)
You can also use Sprockets helper methods with the sass-rails gem.
-
Install the sass-rails gem.
-
Use Ruby code in the stylesheets in the same way as in the view files. For example,
url('../images/rails.png')
becomes
url(asset-path('rails.png', image))
Note: Use a hyphen instead of an underscore.
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.
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
-
Add the line
config.assets.enabled = false
to theconfig/application.rb
file.
For example:module Listr class Application < Rails::Application # Disable the asset pipeline config.assets.enabled = false
-
Save and commit the change.
##More information
For more information about the asset pipeline, see the Rails Guides.