application.haml 和 application.html.haml 之间的区别
Difference between application.haml and application.html.haml?
很多天以来,我试图理解为什么像这样的简单链接:
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>
.所以当 您有没有格式的通用模板,它将适用于所有 格式。
相关文章:
- 使用application/x-www-form-urlencoded使用node.js在post请求中发送数组
- 很棒的共享按钮application.coffee文件在哪里
- 从Haml调用外部javascript中的ruby数组
- 尝试在expressJS应用程序中使用haml咖啡时出现奇怪错误
- 使用 HAML 的复杂 ERB 替换在 JS 中失败
- Rails的问题,haml + javascript在字段对象的模糊
- 在HAML中动态生成的Javascript
- 如何在js.haml文件中编写外部.js文件的内容(RubyonRails)
- 将rails路由调用到CSS(在haml中)
- HAML Javascript Rails if conditions
- Symfony Twig+Haml标记属性检查和属性脚本
- 通过服务工作者提供OpenSearch“application/x-建议+json”
- jscroll pane in web application
- caching issue with web application developed using reactjs &
- 在spring-boot中使用javascript在application.properties文件中获取值
- 将haml-js转换为slim
- 如何将数据/变量从file_controller.rb传递到application.js
- 无法将标头内容类型更改为application/json
- 对GET application/x-jjavascript的Ajax jquery调用
- application.haml 和 application.html.haml 之间的区别