venerdì 29 aprile 2011

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 my-app
by default 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:
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.
Now download the file rails.js from http://github.com/rails/jquery-ujs 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:
<%= 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!

Nessun commento:

Posta un commento