gpt4 book ai didi

ruby-on-rails - HAML 阻止模板引擎呈现 HTML 以外的任何内容

转载 作者:行者123 更新时间:2023-12-04 13:08:39 24 4
gpt4 key购买 nike

我正在使用 Jbuilder(我也尝试使用 Rabl)来渲染 json。
当我尝试在我的应用程序中呈现 jbuilder 模板时,它呈现
内的模板布局/应用文件并返回 HTML 作为 JSON(请参阅“在布局/应用程序内”行):

Rides controller on Github

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)

我不知道我对我的应用程序做了什么,它没有呈现
模板正确。有任何想法吗?

最佳答案

天哪,我花了大约两天的时间才发现导致此问题的问题是
我的布局/应用程序文件名为 aplication.haml .

现在,当我想使用 Builder for xml 或 Rabl for json 等模板引擎时,它尝试在 layouts/application.haml 中的 yield 字段中呈现模板。文件,从而将 html 作为 JSON 返回

我发现问题出在application.haml文件

将其命名为 application.html.haml 解决了问题...

关于ruby-on-rails - HAML 阻止模板引擎呈现 HTML 以外的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10425064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com