HAML prevents template engines to render anything else than HTML
Asked Answered
S

1

6

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?

Spheroidicity answered 3/5, 2012 at 4:18 Comment(0)
S
22

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...

Spheroidicity answered 4/5, 2012 at 5:47 Comment(4)
Wow I was having the same problem! I was trying to implement Jbuilder with no luck, switched to RABL and was having the same program. Renaming application.haml to application.html.haml did the trick! Thanks.Exercise
Upvoted. This solved my problem and took hours to find this posting.Cue
Upvoted. This solved my problem and took hours to find this posting.Cue
Thanks for sharing the solution, we had the same problem. Took few hours to find your post.Barton

© 2022 - 2024 — McMap. All rights reserved.