我想送我的前端应用程序的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