【发布时间】:2014-05-25 23:38:47
【问题描述】:
感谢一些研究和堆栈溢出的一些帮助,我非常接近在我的 rails 应用程序中获得所需的效果。我有一个烦人的错误,我不明白是什么原因造成的。下面我有一个 .each 方法,它从我的 rails 应用程序创建的 _task.html.erb 文件中呈现任务。我试图根据设置的优先级对不同的任务进行颜色编码,但我无法进入最后一步。多亏了 craig.kaminsky,他让我免于尝试使用 AJAX 来完成这项工作。然而,jQuery 不工作,但它应该。
如下所示,我已将脚本标签放置在正确的位置,并正确引用了text/javascript。由于资产管道,这个 jQuery 应该可以工作,但它不能。我不知道什么给了。
<% @tasks.each do |task| %>
<% if task.importance == "Low" %>
<script type="text/javascript">
$("td").addClass("green");
</script>
<% elsif task.importance == "Medium" %>
<script type="text/javascript">
$("td").addClass("orange");
</script>
<% elsif task.importance == "High" %>
<script type="text/javascript">
$("td").addClass("red");
</script>
<% end %>
<tr>
<td><%= task.name %></td>
<td><%= task.description %></td>
<td><%= task.start %></td>
<td><%= task.finish %></td>
<td><%= task.importance %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Remove', task_path(task), method: :delete, data: { confirm: 'Are
you sure you would like to Delete? Did you complete the task?' } %></td>
</tr>
<% end %>
我错过了他们的愚蠢错字吗?最后,我收到的错误是:
Uncaught ReferenceError: $ is not defined
-----------------编辑----------
这是我的 application.js 文件
// This is a manifest file that'll be compiled into application.js, which will
include all the files listed below.
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts,
vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a
relative path.
// It's not advisable to add code directly here, but if you do, it'll appear at the
bottom of the compiled file.
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-
directives) for details
// about supported directives.
//= require jquery
//= require jquery_ujs
//= require foundation
//= require_tree .
$(function(){ $(document).foundation(); });
【问题讨论】:
-
你能出示你的
app/assets/javascripts/application.js文件吗? -
感谢紫舍的回复。我现在就展示出来。
-
好像是文件,不知道是什么问题。
-
你确定你的 jQuery 是先加载的吗?我的意思是使用萤火虫。
-
您正在使用未准备好文档的 jQuery 选择器。使用 firebug,您必须检查 DOM 并验证您的 jQuery 是否首先从头部加载。就我个人而言,我会接受 Subtletree 的回答是正确的。
标签: jquery ruby-on-rails erb