【问题标题】:Rails 4 CoffeeScript not executing when precompiledRails 4 CoffeeScript 预编译时不执行
【发布时间】:2016-10-14 05:58:01
【问题描述】:

我有一个带有一些 Coffeescript 的 Rails 4 应用程序,主要用于在选择另一个列表中的项目时更新列表 - 非常基本的东西。

该脚本在开发中完美运行。但是,一旦它被预编译,即使我在预编译的资产文件中看到实际代码,它也根本不会运行。

我将代码缩小到一个非常小的范围,所以它所做的只是在组合框被更改但仍然无法正常工作时显示警报。我还从 assets/javascript 目录中删除了所有 .coffee 文件,只留下这一个 Coffeescript 文件和 application.js 文件,以确保与其他 .coffee 文件没有冲突。

我也怀疑 turbolinks,然后我将其删除,但仍然没有任何变化。

应用程序.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 any plugin's vendor/assets/javascripts directory 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/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap-sprockets
//= require_tree .

出席. 咖啡

  $('#season_id').on "change", ->
     alert('Success!')

来自 app/views/attendances/_form.html.erb 的表单

<div class="container">
    <form action="/attendances/get_attendance_sheet" accept-charset="UTF-8" data-remote="true" method="post"><input name="utf8" type="hidden" value="&#x2713;" />       <table class="table">
            <tr>
                <td>Saison:</td>
                <td><select name="season_id" id="season_id" class="form-control"><option value="">Choisir une saison</option><option value="1">Gadbois 2015</option></select></td>
                <td>Entraînement du:</td>
                <td><select name="session_id" id="session_id" class="form-control"><option value="">Choisir une date d&#39;entraînement</option><option value=""></option></select></td>
                <td><input type="submit" name="commit" value="Ouvrir" class="btn btn-success" /></td>
            </tr>
        </table>
</form></div>

【问题讨论】:

  • 糟糕的 turbolinks……每个人都认为这是罪魁祸首。
  • 你能试试$(document).on 'change', '#season_id', -&gt; alert 'Success'吗?

标签: javascript ruby-on-rails ruby-on-rails-4 coffeescript asset-pipeline


【解决方案1】:

终于找到问题了,非常感谢@itsnikolay。问题是由于另一个 javascript 文件引发了异常。通过将它们全部编译到一个文件中,异常将使整个脚本停止运行,从而不执行 Coffescripts。

在这种特殊情况下,这是由于缺少从引导库提供工具提示的 gem rails-assets-tether。

在调试模式下,由于所有 javascript 文件都是独立加载的,因此一个文件的异常不会影响加载其他文件。

【讨论】:

  • 谢谢!完全掩盖了这个事实。 “在开发环境中,由于所有 javascript 文件都是独立加载的,因此一个文件的异常不会影响加载其他文件。”系绳例外是罪魁祸首。
【解决方案2】:

检查你编译的javascript文件(http://yousite.net/application-[DIGEST].js。是否有season_id?如果season_id存在,那么尝试更改你的代码:

$ ->
  $('#season_id').on "change", ->
     alert('Success!')

【讨论】:

  • 我按照建议进行了更改并得到了编译后的代码: (function() { $(function() { return $('#season_id').on("change", function() { return alert) ('成功!'); }); }); }).call(this);仍然没有发生任何事情。
  • @Pascal 因为页面上有两个season_id,只留下一个
  • @Pascal 如果它不起作用,请尝试更改以下行:$('#season_id').first().on 'change', -&gt;
  • 我只看到一个 season_id。另一个是 session_id。除非您参考 id 和 name 标签,据我所知,它们不应该相互干扰。
  • 尝试在您的 Chrome 控制台$('#season_id') 中执行,并将其结果粘贴到评论中
猜你喜欢
  • 2014-04-06
  • 2011-12-31
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多