- 當(dāng)使用了active_model_serializers后在render 中就可以使用root參數(shù) 用于指定返回的json數(shù)據(jù)的根,注意如果返回的數(shù)據(jù)是數(shù)組此時(shí) root不起作用
render json: User.all, root: 'users'
{
'users': [
]
}
2 提供了三種適配器 默認(rèn)的是attributes,還有json_api 和 json。不同適配器返回不同格式的數(shù)據(jù) 推薦使用 json
attributes 不包含root
json 如果沒(méi)有指定root則 以model的復(fù)數(shù)作為返回?cái)?shù)據(jù)的key
{
"users": [
{
"id": 1,
"phone": "1506914xxxx"
},
{
"id": 2,
"phone": "1506914xxxx"
}
}
json_api返回的數(shù)據(jù)格式如下
"data": [
{
"id": "1",
"type": "users",
"attributes": {
"phone": "1506914xxx"
}
}
}