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