2012-05-03 59 views
5

我正在使用Jbuilder(我也嘗試使用Rabl)渲染json。 當我嘗試呈現在我的應用程序的JBuilder模板它呈現 模板佈局/應用文件中,並返回HTML爲JSON(請參閱「內佈局/應用」線):HAML阻止模板引擎渲染除HTML之外的其他內容

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) 

我不知道我有我的應用程序,它不渲染 模板完成正確。有任何想法嗎?

回答

20

天哪,我花了大約兩天的時間才發現導致此問題的問題是 我的佈局/應用程序文件被命名爲aplication.haml

現在,當我想用​​一個模板引擎就像生成器XML或Rabl的對JSON,它試圖使我發現,在layouts/application.haml文件中高產田內的模板,並返回HTML作爲JSON

問題就出在application.haml文件

其命名爲application.html.haml解決了這個問題...內

+0

哇,我有同樣的問題!我試圖在沒有運氣的情況下實現Jbuilder,轉而使用RABL並擁有相同的程序。將application.haml重命名爲application.html.haml取得了訣竅!謝謝。 – brent

+0

Upvoted。這解決了我的問題,並花了數小時才能找到這篇文章。 – robbie613

+0

Upvoted。這解決了我的問題,並花了數小時才能找到這篇文章。 – robbie613