When I get a JSON payload from my Rails heroku site (this is a basic simple test site made with the ‘generate scaffold’ command), I get something like this:
[
{"client":
{"name":"Fred Flintstone",
"id":3,
"room":"333",
},
{"client":
{"name":"Wilma Flintstone",
"id":4,
"room":"334",
}
}
]
I would really like to get something more KVC compliant, like this:
{"clients":
[
{"client":
{"name":"Fred Flintstone",
"id":3,
"room":"333",
},
{"client":
{"name":"Wilma Flintstone",
"id":4,
"room":"334",
}
}
]
}
Does anyone know how to do this? Is there some code on the Ruby side or on Heroku’s side that will give me this format?
Kurt
In the controller, instead of
do
or
This doesn’t have anything to do with heroku.
You can turn on/off whether or not the resulting object will have the name of the object as a top level key using:
But as far as I know, there is no configuration option to do what you are suggesting.
See http://apidock.com/rails/ActiveRecord/Serialization/to_json
Another option, if you wish to do this in more than one controller, is to move this behavior to a helper and use that. The logic presented above will work regardless of the model in question.
ie:
then