Capistrano staging/production/demo recipe for precompiling assets

Problem

You have upgraded to rails 3.1.x, 3.2.x and you want capistrano to automatically precompile your assets and do the deployment in your various deployment environments (staging. production, demo etc).

Solution

First add a new directory in your server’s shared folder named assets

Then add the following line to your Capfile:

And finally have your deploy environment file (deploy/staging.rb | deploy/prodution.rb) as follows:

Javascript file not compiled in production with Rails asset pipeline

Problem

You have updated your application to the new 3.1.x or 3.2.x rails, and you are using the new asset pipeline.
You have a javascript file (example.js) that is only called in a specific view or conditionally (especially for css).
The application works fine in the development environment but when deploying in the production (or staging) server, you get an error like the following in the log file:

Solution

As the new asset pipeline puts everything together in one file, your file that is called from a different place cannot be found.
Even if you include your example.js in the application.js manifest file with a //= require example, you still get the same error.
It is possible to compile a single javascript file separately from all the other ones.
So what you have to do is include the following into your necessary environment file (staging.rb, production.rb etc), or in the application.rb file if it going to be used in more than one environments.

Make sure that you include the .js in the code above, and then redeploy with capistrano cap staging|production deploy.