HAML prevents template engines to render anything else than HTML

▼魔方 西西 提交于 2019-12-01 02:51:09

问题


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'):

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)

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?


回答1:


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.

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

I found out that the problem lies within the application.haml file

naming it application.html.haml solved the problem...



来源:https://stackoverflow.com/questions/10425064/haml-prevents-template-engines-to-render-anything-else-than-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!