application.haml 和 application.html.haml 之间的区别

Difference between application.haml and application.html.haml?

本文关键字:application haml 区别 html 之间      更新时间:2023-09-26

很多天以来,我试图理解为什么像这样的简单链接:

link_to 'My Link', my_path(format: :js), remote: true

总是返回完整的HTML文档,而不是执行位于我的文件中的javascript.js.erb :

alert('hello world')

[...]

经过几个小时的调试,我找到了原因:

当我重命名我的主布局文件时,例如:application.haml它呈现完整的 HTML 文档:

Started GET "/my_path/2.js" for 127.0.0.1 at 2016-03-05 12:28:20 +0100
Processing by MyController#show as JS
  Rendered my_path/show.js.erb within layouts/application (0.1ms)
  Rendered layouts/_sidebar.html.erb (18.9ms)
  Rendered layouts/_headbar.haml (0.5ms)
  Rendered layouts/_flash_messages.html.haml (0.2ms)
  Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 102ms (Views: 59.3ms | ActiveRecord: 2.9ms)

当我重命名我的主布局文件时,例如: application.html.haml它正确执行javascript并运行我的hello world弹出窗口

Started GET "/my_path/8.js" for 127.0.0.1 at 2016-03-05 12:28:34 +0100
Processing by MyController#show as JS
  Rendered my_path/show.js.erb (0.1ms)
Completed 200 OK in 24ms (Views: 21.8ms | ActiveRecord: 0.4ms)

为什么根据我的布局的不同文件名,javascript 行为会有所不同?

正如BroiSatse所说:

这不是javascript的行为,而是rails搜索的方式。 模板。首先,它搜索<action_name>.<templating-engine> 文件,然后用于<action_name>.<format><templateing-engine>.所以当 您有没有格式的通用模板,它将适用于所有 格式。