Rails:更改默认的JSON结构

时间:2014-12-26 16:05:51

标签: ruby-on-rails json ember.js

我是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"}
]}

我该怎么办?谢谢大家。

1 个答案:

答案 0 :(得分:2)

在用户索引控制器中尝试此操作

render json: {users: @users}

,其中

@users = [{id: 1, "firstName": "John","lastName": "Smith"},
          {id: 2, "firstName": "John","lastName": "Red"}]