ruby-on-rails - 为什么 application.html.erb 中的 HTML 以 events.json 格式呈现?
问题描述
http://localhost:3000/events.json预期输出:
[{"id":1,"title":1,"start":"2019-03-25T15:00:00.000Z","end":"2019-03-25T16:15:00.000Z"},{"id":989,"title":989,"start":"2019-07-05T19:45:00.000Z","end":"2019-07-05T21:15:00.000Z"},{"id":1088,"title":1088,"start":"2019-07-22T15:00:00.000Z","end":"2019-07-22T19:30:00.000Z"},{"id":1048,"title":1048,"start":"2019-07-20T10:45:00.000Z","end":"2019-07-20T12:15:00.000Z"}]
http://localhost:3000/events.json实际输出:
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<title>Calendarapp</title>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="dm0qaJabzZta3zDAlNSagh5+MIATSeuM7PaPX0ZdGSz85uNRlAiDJhcgRYNfo/tlXXyvyohstDXm+cl41ogXDg==" />
<link rel="stylesheet" media="all" href="/assets/application.self-89e024dae83e1b9c5c55c6f8d1585709357b734c642708e3424091fab88acf94.css?body=1" data-turbolinks-track="reload" />
<script src="/assets/application.self-d658c445b9e450375e95f860f2a2d2412371584b5576f046699d0650acd1399c.js?body=1" data-turbolinks-track="reload"></script>
</head>
<body>
[{"id":1,"title":1,"start":"2019-03-25T15:00:00.000Z","end":"2019-03-25T16:15:00.000Z"},{"id":989,"title":989,"start":"2019-07-05T19:45:00.000Z","end":"2019-07-05T21:15:00.000Z"},{"id":1088,"title":1088,"start":"2019-07-22T15:00:00.000Z","end":"2019-07-22T19:30:00.000Z"},{"id":1048,"title":1048,"start":"2019-07-20T10:45:00.000Z","end":"2019-07-20T12:15:00.000Z"}]
</body>
</html>
活动index.json.jbuilder
代码:
json.array! @events do |event|
json.id event.id
json.title event.id
json.start event.starts_at
json.end event.ends_at
end
知道为什么application.html.erb
HTML 会被渲染,而它不应该被渲染吗?我怎样才能防止这种情况?
解决方案
问题是我使用的是 HAML。
不要命名文件application.haml
。它应始终包含html
扩展名,例如application.html.haml
推荐阅读
- python - 我有一个 3D numpy 直方图及其边缘。如何将其写入带有在轴信息中编码的边缘的 .fits 文件?
- csv - 如何将 CSV 文件加载到 Apache Kylin
- android - Android在应用程序中使用shell或命令创建文件夹
- python - 将超链接绑定到循环中的检查按钮,导致仅绑定最终 url
- javascript - 如何创建一个只有分享链接的 Facebook 分享按钮?
- r - 使用 Shiny R 将反应式弹出图/绘图添加到传单地图
- php - 空购物车重定向到特定页面
- chatbot - 我正在实施一个行动表单来收集一些处理所需的所有数据,但我收到了这个错误:
- java - cURL 在访问 GitHub /search/users API 时有效,但使用 restTemplate.exchange 返回零用户
- unity3d - Unity Shader Graph中的Worldspace 2d纹理置换