Rails: changing the default JSON structure
I am new to Rails and I am using version 4.2.0.
Suppose we have the following JSON in url localhost: 3000 / users
[{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}]
but that's not what i want, i expect something like:
{ users :[
{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}
]}
How can i do this? Thanks everyone.
+3
Francesca
source
to share
1 answer
try this in your user indexing controller
render json: {users: @users}
Where
@users = [{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}]
+2
pkrawat1
source
to share