HAML阻止模板引擎呈现HTML以外的任何内容 [英] HAML prevents template engines to render anything else than HTML
问题描述
我正在使用Jbuilder(并且我也尝试使用Rabl)来呈现json. 当我尝试在应用程序中呈现jbuilder模板时,它将呈现 layouts/application 文件中的模板,并以JSON格式返回 HTML (请参见在布局/应用程序中"行):
I am using Jbuilder (and I also tried to use Rabl) to render json. When I try to render the jbuilder template in my application it renders the template within the layouts/application file and returns HTML as JSON (see line 'within layouts/application'):
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
调用时,一切将按预期进行.
当我创建一个新的应用程序并尝试执行相同的操作时,所有操作也都按预期进行:
However, when I render the json without a template, and do a render json: @ride.as_json
call, things work as expected.
When I create a new application and I try to do the same thing, everything works as expected as well:
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)
我不知道我对应用程序做了什么,因为它没有呈现 模板正确.有什么想法吗?
I don't know what I have done with my application that it is not rendering the templates correctly. Any ideas?
推荐答案
天哪,我花了大约两天的时间才发现导致此问题的原因是
我的布局/应用程序文件名为aplication.haml
.
Gosh, it took me about two days to find out that the issue that is causing this problem is
my layouts/application file which was named aplication.haml
.
现在,当我想使用诸如XML的Builder或json的Rabl的模板引擎时,它试图在layouts/application.haml
文件的yield字段中呈现模板,并因此将html作为JSON返回
Now when I wanted to use a template engine like Builder for xml or Rabl for json, it tried to render the template within the yield field in the layouts/application.haml
file and thus returning html as JSON
我发现问题出在application.haml
文件
将其命名为application.html.haml解决了该问题...
naming it application.html.haml solved the problem...
这篇关于HAML阻止模板引擎呈现HTML以外的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!