为什么 Rails 在 CoffeeScript 文件不能包含 javascript 代码时使用 .js.coffee 扩展名?

发布于 2024-12-28 18:13:03 字数 83 浏览 2 评论 0原文

我发现这相当具有误导性,因为我认为它表明此类文件可能包含 javascript 和 Coffeescript 代码的混合。我是否缺少一些非常基本的东西?

I found that to be rather misleading as I thought it suggest that such files could include a mixture of both javascript and coffeescript code. Is there something very fundamental that I am missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2025-01-04 18:13:03

Rails 中的常见做法是模板具有 .js.coffee.html.erb.html.haml 等扩展名。

如果我没记错的话,Rails 将这些扩展解释为 .[format].[builder] 并使用这些知识来做两件事:

  1. 通过比较 [format] 值找到正确的模板具有请求中列出的可接受的格式接受标头;
  2. 通过 [builder] 值找到合适的模板处理器来解析您的模板。

It's a common practice in Rails for templates to have extensions like .js.coffee, .html.erb, .html.haml, etc.

If I remember correctly Rails interprets these extensions as .[format].[builder] and uses that knowledge to do two things:

  1. find proper template by [format] value comparing it with acceptable formats listed in the request's Accept header;
  2. find appropriate template processor by [builder] value to parse your template.
紅太極 2025-01-04 18:13:03

如果使用反引号,则可以包含纯 JavaScript。

`function(){ do some };` 之类的东西会起作用。

And you can include plain javascript if you use backticks.

Something like `function(){ do something };` would work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文