application.haml 和 application.html.haml 的区别?
Difference between application.haml and application.html.haml?
很多天以来,我一直在努力理解为什么像这样一个简单的 link :
link_to 'My Link', my_path(format: :js), remote: true
总是返回完整的 HTML 文档,而不是执行位于我的 file.js.erb 中的 javascript :
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 popup :
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 所说:
This is not javascript behaviour, it is how rails searches for
templates. First it search for <action_name>.<templating-engine>
files, then for <action_name>.<format><templateing-engine>
. So when
you have generic template without the format, it will be taken for all
the formats.
很多天以来,我一直在努力理解为什么像这样一个简单的 link :
link_to 'My Link', my_path(format: :js), remote: true
总是返回完整的 HTML 文档,而不是执行位于我的 file.js.erb 中的 javascript :
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 popup :
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 所说:
This is not javascript behaviour, it is how rails searches for templates. First it search for
<action_name>.<templating-engine>
files, then for<action_name>.<format><templateing-engine>
. So when you have generic template without the format, it will be taken for all the formats.