Rails 3.1 Asset Pipeline




Rails Coach show

Summary: Assets in Rails before version 3.1 were kept in the /public folder. In Rails 3.1 they've been moved to /app/assets and function in a slightly different way. Here are some of the highlights: Javascript assets written in Coffeescript will now be compiled to Javascript. CSS assets written in SASS or SCSS will now be compiled to CSS Other templating languages like ERB can be used in assets JQuery is now the default Javascript framework in Rails 3.1 You can combine Javascript files with the new directives Pre-compiled assets When compiling from SASS, SCSS, CoffeeScript, or ERB, you simply add the appropriate extension to your .js or .css file. (.sass, .scss, .coffee, or .erb respectively) To combine several Javascript files into one file, (for example application.js) just do something like this. //= require jquery //= require jquery_ujs //= require_tree . //= require_directory tooltips   For several CSS files, it's the same commands, just *= instead of //= and wrapped in /* ... */. Here's a quick example: /* *= require reset *= require base *= require_tree . *= require_directory tooltips */   require pulls in a file. require_tree gets everything in the specified path. require_directory pulls in all the files in a given directory (no nesting). You can precompile your assets by running bundle exec rake assets:precompile (For example, if your server doesn't have a Javascript runtime on it capable of compiling Coffeescript.) Here are some resources for CoffeeScript: The Coffeescript Website CoffeeScript Basics (video) CoffeeScript: The Cool Parts (video)