Rails 3.1 中的 Coffeescript 和 Haml 以及不显眼的 Javascript(数据远程)
我搜索了 le interwebs,但没有发现有人遇到与我相同的问题,所以我在这里提出我的问题。
我刚刚开始使用 Rails 3.1 与 Compass、Haml 和 CoffeeScript,但遇到了问题。当我将位于 app/assets/javascript/index.js
中的特定于控制器的 JavaScript 文件重命名为 index.js.coffee
并将 JavaScript 代码转换为 CoffeeScript 时,一切正常正如预期的那样 - 该文件由浏览器请求并即时编译为 JavaScript。 CoffeeScript 文件中的更改也会触发重新编译。
但是,当我尝试使用不显眼的 JavaScript (:remote => true
) 执行此操作并重命名位于视图文件夹 app/views/index/index.js 中已经工作的 JavaScript 文件时.haml
到 index.js.coffee.haml
并翻译包含的代码,Rails 不会将其识别为需要编译的 CoffeeScript。
我做错了什么?我是否必须主动为视图启用 CoffeeScript 评估?在哪里?
I searched le interwebs, but I haven't found someone experiencing the same problem as me, so I propose my question here.
I just started using Rails 3.1 with Compass, Haml and CoffeeScript and ran into a problem. When I rename my controller-specific JavaScript file located in app/assets/javascript/index.js
to index.js.coffee
and translate the JavaScript code to CoffeeScript, everything works as expected - the file is requested by the browser and compiled on the fly into JavaScript. Changes in the CoffeeScript file do also trigger recompilation.
However, when I try to do this with unobtrusive JavaScript (:remote => true
) and rename the already working JavaScript file located in the view folder app/views/index/index.js.haml
to index.js.coffee.haml
and translate the included code, Rails doesn't recognize it as a CoffeeScript that needs to be compiled.
What am I doing wrong? Do I actively have to enable CoffeeScript evaluation for the view? Where?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所提到的,Rails 3.1 中引入的资产管道将自动为您将 CoffeeScript 资产编译为 javascript。目前视图的情况并非如此:只会编译 app/assets/javascripts 中的文件。
要在视图中使用 Coffeescript,您需要使用 https://github.com/markbates/coffeeBeans 暂时。
更新:根据下面 Kyle Heironimus 的评论,似乎这个功能可以由 coffe-rails 提供(参见 https: //github.com/rails/coffee-rails 和 http://rubygems.org/gems/coffee-rails )
The asset pipeline introduced in Rails 3.1 will automatically compile coffeescript assets into javascript for you, as you've mentioned. This is currently NOT the case for views: only files in app/assets/javascripts will be compiled.
To use Coffeescript in views, you'll need to use https://github.com/markbates/coffeeBeans for the time being.
Update : per Kyle Heironimus' comment below, it seems this functionality can be provided by coffe-rails (see https://github.com/rails/coffee-rails and http://rubygems.org/gems/coffee-rails )