Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 500
Brian Joseph Spinos Avatar asked Dec 05 '22 13:12

Brian Joseph Spinos


1 Answers

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.

like image 159
ChoKim Avatar answered Dec 25 '22 15:12

ChoKim