How can I use Jquery in rails 3.2.8?

2019-03-31 01:18发布

问题:

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?

回答1:

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.



回答2:

You can write a jquery code and put file as file_name.js in Javascript folder and call this file in your layout file. You can refer http://jqueryui.com/



回答3:

You need the gem. Maybe this will help you:

http://c.kat.pe/post/how-to-use-jquery-for-rails-3/