Davide Targa website

Programming, web development and open source

jQuery on Rails 3

Ruby on Rails is an open source web application framework for the Ruby programming language. The last version of this framework is the 3.0.0 released Aug 29, 2010. By default, when you start a new Rails application with rails new appname it uses prototype as javascript framework. Now prototype isn’t the best solution for all developers so you might want to use jQuery instead.

To use jQuery you need to create a new rails application with the following command instead:

1
$ rails new my-app -J

Notice the -J option that skips the creation of the prototype files in the new app.

Now you have to download jQuery from its website and put the jquery-x.x.x.js file inside the public/javascripts folder of your application. Download the file rails.js from github and put it inside the public/javascripts folder too. Now you need to add a reference to jQuery inside the application layout. To do so, open the file app/views/layouts/application.html.erb and add the following code:

1
2
<%= stylesheet_link_tag 'jquery-ui-1.8.5', 'scaffold' %>
<%= javascript_include_tag 'jquery-1.4.3', 'jquery-ui-1.8.5', 'application' %>

You’re done! Now you can use Ruby on Rails with jQuery.

Happy coding!

Comments