Ruby on Rails JavaScript and CSS Assets

Created: 17 April 2012  Modified:

Learned today that CSS and JavaScript Assets in Rails 3 are minified automatically for you. Additionally the application.js and application.css can take “require” statements and include any JavaScript into one file called application.js or application.css. I am guessing the logic here is that fewer javascript includes makes for faster page loading. An example is shown below.

mysecurity/app/assets/javascripts/application.js

// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree .

The “//= require jquery” includes the jQuery JavaScript APi which is included as a gem file. The “//= require_tree .” seems to include all javascript files into the page. I will be taking out the require_tree which was placed in as default.

tags:
   Less Is More