将应用程序JS转换为CoffeeScript

时间:2017-10-14 05:39:29

标签: ruby-on-rails coffeescript

我借助nested_form_for gem在我的rails应用程序中创建了一个嵌套表单。我试图将application.js转换为application.coffee。但是我遇到了针对nested_form_for gem的以下问题:

  

无法找到文件' jquery_nested_form'使用类型' application / javascript'

以下是我的application.coffee内容:

#= 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 个答案:

答案 0 :(得分:1)

你在这里遇到了一些问题。

首先,你在application.coffee的第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'
在您的Gemfile中

,然后运行:

$ bundle install

在您的Rails根目录中。

最后,作为Ruby on Rails风格的一般要点,将application.js转换为application.coffee很少是必要或有帮助的。此文件只是一个清单,告诉Rails在编译的application.js中包含哪些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.