初心者の質問があります。
以下のRubyコードは、Output1に示されている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"
}
}
]