我有一个带有“active_model_serializers”gem的rails api。
http://localhost:3000/api/buildings/30.json为我生成了这个:
{
building:{
id:30,
city_name:"msc",
infrastructure:[
{
id:40,
name:"name 1",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
},
{
id:69,
name:"name 2",
created_at:"2015-07-30T08:26:50.000Z",
updated_at:"2015-07-30T08:26:50.000Z"
},
{
id:39,
name:"name 3",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
}
]
}
}
我也将EmberJs与ActiveModelAdapter一起使用。但是Ember希望如此:
{
building:{
id:30,
city_name:"msc",
infrastructure:[
40,
69,
39
]
},
infrastructure:[
{
id:40,
name:"name 1",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
},
{
id:69,
name:"name 2",
created_at:"2015-07-30T08:26:50.000Z",
updated_at:"2015-07-30T08:26:50.000Z"
},
{
id:39,
name:"name 3",
created_at:"2015-07-30T08:26:49.000Z",
updated_at:"2015-07-30T08:26:49.000Z"
}
]
}
如何让ember使用rails json结构? THX!
答案 0 :(得分:1)
你需要使用Ember方面的Embedded Records Mixin。
App.ColorSerializer = DS.ActiveModelSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
foos: {embedded: 'always'}
}
});
答案 1 :(得分:0)
另一个选择是使用JSON API路由。
我认为这是一个更好的选择,因为
我写了一个位于http://emberigniter.com/modern-bridge-ember-and-rails-5-with-json-api/的指南(对于Rails 5),但你肯定可以将它改编为你的Rails版本