如何使用基础将jQuery添加到一个Erb文件中?
多亏了一些研究和stack overflow的一些帮助,我非常接近在rails应用程序中获得所需的效果。我有一个恼人的错误,我不明白是什么导致了这一点。下面我有一个.each方法,它从我的rails应用程序创建的如何使用基础将jQuery添加到一个Erb文件中?,jquery,ruby-on-rails,erb,Jquery,Ruby On Rails,Erb,多亏了一些研究和stack overflow的一些帮助,我非常接近在rails应用程序中获得所需的效果。我有一个恼人的错误,我不明白是什么导致了这一点。下面我有一个.each方法,它从我的rails应用程序创建的\u task.html.erb文件中呈现一个任务。我试图让不同的任务根据其设置的优先级进行颜色编码,但我无法进入最后一步。多亏了craig.kaminsky,他让我从最初尝试使用AJAX做到这一点中省去了很多心痛。但是,jQuery不起作用,但它应该起作用 如下图所示,我已经准备好了脚
\u 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 %>
-----------------编辑---------------------
这是我的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(); });
您可以使用rails助手方法来插入类服务器端,而不是jquery 例如 任务\u helper.rb
def get_importance_class(importance)
case importance
when 'low'
"green"
when 'medium'
"orange"
when 'high'
"red"
else
""
end
end
在你看来,你可以做:
<% @tasks.each do |task| %>
<% importance_class = get_importance_class(task.importance) %>
<tr>
<td class="<%= importance_class %>"><%= task.name %></td>
<td class="<%= importance_class %>"><%= task.description %></td>
<td class="<%= importance_class %>"><%= task.start %></td>
<td class="<%= importance_class %>"><%= task.finish %></td>
<td class="<%= importance_class %>"><%= task.importance %></td>
<td class="<%= importance_class %>"><%= link_to 'Edit', edit_task_path(task) %></td>
<td class="<%= importance_class %>"><%= 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 %>
如果您要将该类添加到每个单元格(
td
),那么您可能希望将其应用到该行能否显示您的app/assets/javascripts/application.js
文件?谢谢您的回复。我现在就展示它。似乎文件,不知道是什么问题。你确定你的jQuery是先加载的吗?我的意思是使用firebug。您使用的是jQuery选择器,而没有准备好文档。使用firebug,您必须检查DOM并验证jQuery是否首先从head加载。我个人认为子树的答案是正确的。我会试试这个。有一件事,我应该把这个helper方法放在哪里?当你生成一个控制器时,它会在app/helpers/*controller\u name*\u helper.rb中创建一个对应的helper文件。因此,您可能有一个tasks\u helper.rb
文件。你把它放在哪个助手文件里并不重要,它们只是帮助你保持事情的条理性。您将有一个可以使用的application\u helper.rb
文件。感谢帮助子树,尽管我遇到了循环依赖项错误,但它仍然无法工作。不过我很感激。嗯,那样的话,我不确定问题出在哪里。上面粘贴的代码在我的测试中非常有效。您使用的是什么版本的rails?尝试重置服务器,并确保没有两个同名文件。您是否在任何视图/控制器/帮助程序中使用了include
。我不知道发生了什么,但这次我成功了。这一定是我的错别字。感谢您向我介绍帮助器模块和方法。非常感谢。
<% @tasks.each do |task| %>
<% importance_class = get_importance_class(task.importance) %>
<tr>
<td class="<%= importance_class %>"><%= task.name %></td>
<td class="<%= importance_class %>"><%= task.description %></td>
<td class="<%= importance_class %>"><%= task.start %></td>
<td class="<%= importance_class %>"><%= task.finish %></td>
<td class="<%= importance_class %>"><%= task.importance %></td>
<td class="<%= importance_class %>"><%= link_to 'Edit', edit_task_path(task) %></td>
<td class="<%= importance_class %>"><%= 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 %>