Saturday, April 16, 2011

Replace Prototype with jQuery in Rails 3

Note: I'm using Rails 3.0.3.

I decided to use jQuery instead of Protoype on my personal Rails 3 project.  At first, I naively thought that I could simply include jQuery instead of Prototype, and everything would run just fine.  Instead, I noticed that Chrome was reporting an error in rails.js, which eventually led me to Josh Huckabee's blog post, as well as the official jquery-rails gem.

The installation instructions on the github page are straightforward and simple, so I won't reproduce them here.  After you add the gem to your Gemfile and install it, and you run the command:
rails generate jquery:install
it will pull down the latest version of jQuery.   You should answer "yes" when it asks if you want to overwrite rails.js.

I modified my default layout (application.html.erb) to include this line:
  <%= javascript_include_tag "jquery-1.5.2.min", "", "rails", "application" %>

Of course, you might not need some of these, and you can include additional files as well.

No comments: