Rails 3.1 — use the jQuery Google Library instead of the default

Stamped: 2011-05-19 00:00:00 -0400 | Show comments

Rails 3.1 supports all sorts of fun new technologies out of the box, Coffeescript, Sprockets and more. However, the default jQuery 1.6.1 library that is included by default isn't minified, as well as the jQuery-rails library. This results in a whopping 250kb javascript overhead.

To reduce the overhead, and use the Google jQuery library instead, you have to make a couple changes. First, in app/assets/javascripts/application.js:

//= require jquery
//= require jquery_ujs
//= require_tree .

Change this to:

//= require jquery_ujs
//= require_tree .

And in your app/views/layouts/application.html.* change the header by adding an additional line:

    <%= stylesheet_link_tag    "application" %>
    <%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" %>
    <%= javascript_include_tag "application" %>
    <%= csrf_meta_tags %>

Note, you can't just add //= require https://../jquery.min.js because sprockets builds the resulting js file each time Rails receives a request (assuming it has changed). It really wouldn't make much sense to download the api and redistribute it, it would defeat the point of even using it in the first place.

All the additions in Rails 3.1 are wonderful, go try them out!

tags: rails, jquery
recent entries
Rails — A faster way for next and previous links on a post, article, or any model
The awkward things Siri says
Node.js — Getting oAuth Up and Running Using Express.js and Mongoose
Node.js — Getting oAuth Up and Running Using Express.js, Railway.js and Mongoose
Migrating from Rails 3.1 RC4 to RC5 using Heroku's Cedar Stack (also compass, unicorn, and sendgrid)
Random Freeze Fix for GTX 460 in 10.6 (osx86)
Wasted on Steam - an analytic tool for the Steam platform
Rails 3.1 — SQL logging to STDOUT during testing (with rspec, test::unit, or cucumber)
Rails 3.1 — Using ERB/HAML/etc within a Coffeescript JS file
Rails 3.1 — 'load_missing_contant': Expected ... to define ... (LoadError)
View the entire archive of articles