首页 > 解决方案 > 为什么 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.erbHTML 会被渲染,而它不应该被渲染吗?我怎样才能防止这种情况?

标签: ruby-on-rails

解决方案


问题是我使用的是 HAML。

不要命名文件application.haml。它应始终包含html扩展名,例如application.html.haml


推荐阅读