代码之家  ›  专栏  ›  技术社区  ›  Vishal

将应用程序JS转换为CoffeeScript

  •  0
  • Vishal  · 技术社区  · 7 年前

    在nested\u form\u for gem的帮助下,我在rails应用程序中创建了一个嵌套表单。我试图转换应用程序。js到应用程序。咖啡但对于gem的nested\u form\u,我面临以下问题:

    找不到类型为“application/javascript”的文件“jquery\u nested\u form”

    以下是我的申请。咖啡含量:

    #= require jquery
    #= bootstrap.min.js
    #= owl.carousel.min.js
    #= require jquery_ujs
    #= require jquery-ui
    #= require jquery_nested_form
    #= require_tree ./controllers
    #= require_tree .
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   drosboro    7 年前

    你这里有几个问题。

    首先,您在应用程序的第2行和第3行缺少一些“要求”。咖啡这些线路:

    #= require jquery
    #= bootstrap.min.js
    #= owl.carousel.min.js
    

    应成为:

    #= require jquery
    #= require bootstrap.min.js
    #= require owl.carousel.min.js
    

    第二,你的 couldn't find file 'jquery_nested_form' 错误很可能是因为 nested_form gem当前不在您的捆绑包中。确保你有这样一句话:

    gem 'nested_form'
    

    然后运行:

    $ bundle install
    

    最后,正如Ruby on Rails风格的一般观点一样,很少需要或有帮助地转换 application.js application.coffee . 这个文件只是一个清单,告诉Rails编译后的脚本中要包含哪些javascript/coffeescript文件 . 由于通常不建议您向该文件中添加代码,因此将其转换为coffeescript似乎没有多大价值。完全可以将此文件保留为javascript,并将所有代码编写在单独的coffeescript文件中。从默认文件注释中:

    // This is a manifest file that'll be compiled into application.js, which will include all the files
    // listed below.
    
    // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
    // compiled file. JavaScript code in this file should be added after the last require_* statement.