我是Rails中的新手,我使用的是版本4.2.0。
假设我们在url localhost上有以下JSON:3000 / users
[{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}]
但这不是我想要的,我期待的是:
{ users :[
{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}
]}
我该怎么办?谢谢大家。
答案 0 :(得分:2)
在用户索引控制器中尝试此操作
render json: {users: @users}
,其中
@users = [{id: 1, "firstName": "John","lastName": "Smith"},
{id: 2, "firstName": "John","lastName": "Red"}]