我有一个新手问题。
下面的 Ruby 代码返回输出 1 中显示的 JSON。如何修改它以返回 Output2 中显示的 JSON,即每条记录都在客户记录中。
代码:
def index
@customers = Customer.all
respond_to do |format|
format.html
format.json {render json: @customers.to_json}
end
end
输出1:
[
{
"address":"123 Main Ave, Palo Alto, CA",
"created_at":"2012-07-10T19:49:24Z",
"id":1,
"name":"ACME Software Inc.",
"phone":"1-650-555-1500",
"updated_at":"2012-07-10T19:49:24Z"
},
{
"address":"555 Art Drive, Mountain View, CA",
"created_at":"2012-07-10T19:50:19Z",
"id":2,
"name":"My Company",
"phone":"1-415-555-1000",
"updated_at":"2012-07-10T19:50:19Z"
}
]
输出2:
[
{
"customer":{
"address":"123 Main Ave, Palo Alto, CA",
"created_at":"2012-07-10T19:49:24Z",
"id":1,
"name":"ACME Software Inc.",
"phone":"1-650-555-1500",
"updated_at":"2012-07-10T19:49:24Z"
}
},
{
"customer":{
"address":"555 Art Drive, Mountain View, CA",
"created_at":"2012-07-10T19:50:19Z",
"id":2,
"name":"My Company",
"phone":"1-415-555-1000",
"updated_at":"2012-07-10T19:50:19Z"
}
}
]