[英]How do I use jQuery in Ruby on Rails 3.2?
I am a newbie in Ruby on Rails and need to know what things we have to be careful about if we are willing to use jQuery in a Ruby on Rails application. 我是Ruby on Rails的新手,并且需要知道如果我们愿意在Ruby on Rails应用程序中使用jQuery,我们需要注意哪些事项。
In my view page I have: 在我的查看页面中,我有:
<script type="text/javascript" src= "https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<%= javascript_include_tag "application" %>
<script>
alert("hussain")
alert($('#hussi').val())
</script>
It gives the first alert as expected, but for the second alert it says '$' is not defined. 它按预期给出第一个警报,但是对于第二个警报,它说未定义“ $”。
I have the jquery-rails gem installed. 我安装了jquery-rails gem。
The browser points out that I have a missing reference in my application.js
file. 浏览器指出,我的application.js
文件中缺少参考。
require jquery;
require jquery_ujs;
I saw some file examples where they mention it like: 我看到了一些文件示例,其中有这样的示例:
= require jquery;
= require jquery_ujs;
But adding'=' raises an IDE error in my IDE. 但是添加'='会在我的IDE中引发IDE错误。
Add this to you application.js: 将此添加到您的application.js:
//= require jquery
//= require jquery_ujs
and add this to your Gemfile: 并将其添加到您的Gemfile中:
gem 'jquery-rails'
Also make sure you have this in you application layout: 还要确保在应用程序布局中包含以下内容:
<%= javascript_include_tag "application" %>
Just a note: avoid using tags in your code, move your javascript to your assets folder. 请注意:避免在代码中使用标签,请将javascript移至资产文件夹。
There is a typo in your code: text/javas
c ript
. 有一个在你的代码一个错字: text/javas
Ç ript
。 This causes the script to not be parsed. 这将导致脚本无法解析。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.