Brian Joseph Spinos Brian Joseph Spinos - 6 months ago 16
Javascript Question

How can I use Jquery in rails 3.2.8?

All the tutorials I've read tell me to use the "public/javascripts" folder. But there is no such folder in rails 3.2.8


  • where do I place my jquery code?

  • isn't Jquery included in Rails 3.2.8?


Answer

The default Rails application (> 3.2) includes the jQuery gem on line 23 of the Gemfile:

gem 'jquery-rails'

You place your jQuery code in a custom JavaScript file--e.g., hello.js--in the following path:

app/assets/javascripts/

Example:

app/assets/javascripts/hello.js

In hello.js, make sure you include your jQuery code inside of this block:

$(document).ready(function() {
    YOUR CODE HERE
});

That's it. You're ready to use jQuery in Rails. (This works because of line 15 in app/assets/javascripts/application.js)

//= require_tree .

This line says to include everything in the javascripts directory.