问题
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