我在update.json.jbuilder
json.data do
json.(@user_group, :id, :name, :description)
json.users @user_group.users, :id, :name
json.roles @user_group.roles, :id, :name
end
给了我这个JSON:
{
"data":{
"id":95,
"name":"Management group",
"description":"This is management group",
"users":[
{
"id":10,
"name":"Test User"
}
],
"roles":[
{
"id":1,
"name":"FULL ACCESS (TEMPORARY)"
}
]
}
}
如何包装"数据"在这个类似的东西中,请?
{
"data": [
{
"id":95,
"name":"Management group",
"description":"This is management group",
"users":[
{
"id":10,
"name":"Test User"
}
],
"roles":[
{
"id":1,
"name":"FULL ACCESS (TEMPORARY)"
}
]
}
]
}
在第一个和最后一个{}之后注意[]
到目前为止,我一直试图实施json.array!
,但没有运气,因为它抛出了我ActionView::Template::Error (undefined method map
,这意味着我试图把可能不适合的东西放在一起{{3} }。
答案 0 :(得分:1)
使用child! method似乎很简单:
json.data do
json.child! do
json.(@user_group, :id, :name, :description)
json.users @user_group.users, :id, :name
json.roles @user_group.roles, :id, :name
end
end