Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3.1 assets pipeline in production

I am using the assets pipeline (in Rails 3.1.3) and am kind of struggling to make it work in production.

Situation

In my /app/assets/stylesheets directory I have the following files:

application.css --> this is the default rails one
stylesheet.css --> This is my custom stylesheet

I spent a lot of time getting my stylesheet.css included in the /public/assets/directory in production (by running rake assets:precompile) and I finally made it by adding the following line into in my application.rb file:

    config.assets.precompile += ['stylesheet.css']

I know have the right precompiled stylesheet.css file in production.

My Problem

The problem I have is when using stylesheet_link_tag with my stylesheet.css file. It turns out:

<%= stylesheet_link_tag "stylesheet" %> is resolved into <link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css"> in production I would expect the path to be resolved into /assets/stylesheet.css just like it does in development.

What is even more surprising is that application.css behaves perfectly even though <%= stylesheet_link_tag "application"%> resolves into <link href="/stylesheets/stylesheet.css" media="screen" rel="stylesheet" type="text/css">. What I don't understand is that the public/stylesheets/ directory does not exist in rails 3.1.

Any idea ?

like image 735
rpechayr Avatar asked Jan 06 '12 15:01

rpechayr


People also ask

What is assets pipeline in rails?

1 What is the Asset Pipeline? The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass, and ERB.

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What does rake assets Clean do?

Two cleanup tasks: rake assets:clean is now a safe cleanup that only removes older assets that are no longer used, while rake assets:clobber nukes the entire public/assets directory. The clean task allows for rolling deploys that may still be linking to an old asset while the new assets are being built.

What does bundle exec rake assets Precompile do?

4.1 Precompiling Assets. Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline to the disk. Compiled assets are written to the location specified in config.


1 Answers

Richard Hulse answers pointed me to the right direction. What happens is really subtle..

The answer to my question is Rails 3.1 assets has no fingerprint in production.

Basically, my project use mongoid instead of ActiveRecord. According to Mongoid documentation about configuration, the application.rb file can be modified to not include ActiveRecord which means removing:

require railties/all

And replacing it with:

require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
# require "sprockets/railtie" # Uncomment this line for Rails 3.1+

I was so used to doing this manipulation with rails 3.0.x that I did not pay attention to the comment related to Rails 3.1

My problem was that I was not requiring sprockets !

Thank you all for your help !

like image 162
rpechayr Avatar answered Sep 28 '22 01:09

rpechayr