我想送我的前端應用程序的JSON看起來像這樣:加載ActiveModel使用串行器::包括兩個父JSON數組
{
facilities: [
{id: 5, name: 'happy days ranch', location: { address: '1424 Pastoral Lane', zipcode: '25245'}, instructor_ids: [2, 4, 9]}
],
instructors: [
{id: 4, name: 'Johnny Pheonix', skill: '8', picture: 'aws_url', facility_ids: [5, 8, 12}
]
}
事情我已經試過
render :json => @facilities
的序列化程序發現這一點。好極了!但是,這並不包括任何教練
render :json => {facilities: @facilities, instructors: @instructors}
這給了我一個教練陣列和陣列的設施,但沒有使用activemodel的串行器::。
render :json => [@facilities, @instructors]
起初我對這個很興奮,因爲它給了我兩個數組,它使用了ActiveModel :: Serializers。然而,這就是JSON的樣子:
{facilities: [
{facilities: [
#my facilities data
]},
{facilities: [
#my instructor data
]}
]}
是什麼我試圖做甚至允許由ActiveModel :: Serializers?如果是這樣,怎麼樣?
非常感謝!
這與我的問題完全相同。謝謝你的回答! – 2013-07-20 20:44:32
謝謝你的解決方案。它可以工作,並且可以通過在渲染器中設置「root:false」來禁用根節點「搜索」。經過審查,我們決定不使用這種方法和2個根元素,我們最終使用了2個獨立的API端點,這使我們的設計更簡單。 – 2014-03-05 18:22:41