如何在Rails 3.2.8中使用Jquery? [英] How can I use Jquery in rails 3.2.8?

查看:49
本文介绍了如何在Rails 3.2.8中使用Jquery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读的所有教程都告诉我使用"public/javascripts"文件夹.但是在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

  • 我在哪里放置我的jquery代码?
  • Rails 3.2.8中不包含Jquery吗?

推荐答案

默认的Rails应用程序(> 3.2)在Gemfile的第23行包含jQuery gem:

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

gem 'jquery-rails'

您将jQuery代码放在自定义JavaScript文件(例如hello.js)中的以下路径中:

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

app/assets/javascripts/

示例:

app/assets/javascripts/hello.js

hello.js中,确保在此块中包含jQuery代码:

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

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

就是这样.您已经准备在Rails中使用jQuery. (由于app/assets/javascripts/application.js中的第15行,因此可以正常工作)

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 .

此行说要包括javascripts目录中的所有内容.

This line says to include everything in the javascripts directory.

这篇关于如何在Rails 3.2.8中使用Jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆