【问题标题】:How to add jQuery to an erb file using Foundation?如何使用 Foundation 将 jQuery 添加到 erb 文件?
【发布时间】: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


【解决方案1】:

您可以使用 rails 辅助方法插入类服务器端而不是 jquery。

例如

tasks_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/helpers/*controller_name*_helper.rb 中创建相应的帮助文件。所以你可能有一个tasks_helper.rb 文件。你把它放在哪个帮助文件中并不重要,它们只是帮助让事情井井有条。您将拥有一个可以使用的application_helper.rb 文件。
  • 感谢 Subltetree 的帮助,尽管我收到循环依赖错误,但它仍然无法正常工作。不过我很感激。
  • 嗯,在这种情况下,我不确定问题出在哪里。上面粘贴的代码在我的测试中完美运行。您使用的是什么版本的导轨?尝试重置服务器并确保您没有 2 个名称相同的文件。您是否在任何视图/控制器/助手中使用include
  • @Subletree 非常感谢。我不知道发生了什么,但这一次我成功了。这一定是我的错字。感谢您向我介绍帮助模块和方法。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多