我正在使用 Jbuilder(我也尝试使用 Rabl)来渲染 json。当我尝试在我的应用程序中呈现 jbuilder 模板时,它会在布局/应用程序文件中呈现模板并将HTML作为 JSON 返回(参见“在布局/应用程序内”行):
Started GET "/random_photo.json"
Processing by RidesController#random_photo as JSON
>> Rendered rides/random_photo.json.jbuilder within layouts/application (0.3ms)
Rendered shared/_banners_in_corners.haml (3.0ms)
Rendered shared/_sign_in_and_out.haml (2.0ms)
Rendered layouts/_navigation.haml (7.3ms)
Completed 200 OK in 156ms (Views: 120.7ms | ActiveRecord: 3.1ms)
但是,当我在没有模板的情况下渲染 json 并进行render json: @ride.as_json
调用时,事情会按预期工作。当我创建一个新应用程序并尝试做同样的事情时,一切都按预期工作:
Started GET "/posts/1.json"
Processing by PostsController#show as JSON
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "1"]]
Rendered posts/show.json.jbuilder (0.6ms)
Completed 200 OK in 20ms (Views: 19.2ms | ActiveRecord: 0.1ms)
我不知道我对我的应用程序做了什么,它没有正确呈现模板。有任何想法吗?