将application.js重命名为application.js.coffee?

Yoa*_*gen 9 ruby-on-rails coffeescript

启动一个新项目,我只是尝试将我的application.js重命名为application.js.coffee.

Rails引发错误:SyntaxError:意外的IDENTIFIER(在my_path/application.js.coffee中)

Application.js为空,只是jquery包括:

//= require jquery
//= require jquery_ujs
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢

apn*_*ing 27

coffeescript中的注释用以下内容#替换:

#= require jquery
#= require jquery_ujs
Run Code Online (Sandbox Code Playgroud)

  • coffeescript真的很棒,你会很快习惯它的惯例.提醒您可以使用`http:// js2coffee.org /`等工具轻松将js转换为咖啡 (2认同)

Rod*_*rek 7

你为什么想这么做?

建议不要在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
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
Run Code Online (Sandbox Code Playgroud)

这个文件不是你的常规.js文件,它的目的是为资源管道提供javascripts,这不是一个在这里添加代码的好习惯.

无论你需要做什么,都可以采取另一种方式......