将应用程序 JS 转换为 CoffeeScript
Converting application JS to CoffeeScript
在 nested_form_for gem 的帮助下,我在我的 rails 应用程序中创建了一个嵌套表单。我试图将 application.js 转换为 application.coffee。但是我遇到了 nested_form_for gem 的以下问题:
couldn't find file 'jquery_nested_form' with type '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 .
你这里有几个问题。
首先,您在 application.coffee 的第 2 行和第 3 行中遗漏了一些 "requires"。这些行:
#= 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 在 Rails 风格上的一般观点,将 application.js
转换为 application.coffee
几乎没有必要或没有帮助。该文件只是一个清单,告诉 Rails 哪些 javascript/coffeescript 文件要包含在已编译的 application.js
中。由于通常不建议您向该文件添加代码,因此将其转换为 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.
在 nested_form_for gem 的帮助下,我在我的 rails 应用程序中创建了一个嵌套表单。我试图将 application.js 转换为 application.coffee。但是我遇到了 nested_form_for gem 的以下问题:
couldn't find file 'jquery_nested_form' with type '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 .
你这里有几个问题。
首先,您在 application.coffee 的第 2 行和第 3 行中遗漏了一些 "requires"。这些行:
#= 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 在 Rails 风格上的一般观点,将 application.js
转换为 application.coffee
几乎没有必要或没有帮助。该文件只是一个清单,告诉 Rails 哪些 javascript/coffeescript 文件要包含在已编译的 application.js
中。由于通常不建议您向该文件添加代码,因此将其转换为 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.