问题
rails 3: include_root_in_json = true allows to have a root property for json response. But it doesn't work with array of object, there is a property like include_root_in_array_json? actually I have this response:
[ {"model": {"a":"a_value"}}, {"model": {"b":"b_value"}}]
but I want a response like that:
{"models":[ {"model": {"a":"a_value"}}, {"model": {"b":"b_value"}}]}
xml response has a root property 'models' which include the array, but json not...
回答1:
You can try:
{models: Model.all}.to_json
It's not a very elegant solution but it does its job fine.
来源:https://stackoverflow.com/questions/9750690/include-root-in-json-for-array