RABL更改根节点名称

时间:2013-02-12 23:58:31

标签: ruby-on-rails ruby rabl

我遇到了rabl的问题,它正在改变嵌套节点名称:

示例,这是我的目标/ index.json.rabl

collection @customers => :targets
extends 'customers/profile'

这是我的客户/ profile.json.rabl

object @customer => :profile
attributes :id, :username, :customer_id, :first_name, :last_name, :avatar_url

命中targets.json(目标#index)时的输出:

{
    "targets": [
        {
            "target": {
                "id": 3,
                "username": null,
                "first_name": "Scott",
                "last_name": "Thomas",
                "avatar_url": null
            }
        },
        {
            "target": {
                "id": 3,
                "username": null,
                "first_name": "Thomas",
                "last_name": "MacKay",
                "avatar_url": null
            }
        }
    ]
}

问题是我希望“targets”数组中的“目标”节点被命名为“profile”。像这样:

{
        "targets": [
            {
                "profile": {
                    "id": 3,
                    "username": null,
                    "first_name": "Scott",
                    "last_name": "Thomas",
                    "avatar_url": null
                }
            },
            {
                "profile": {
                    "id": 3,
                    "username": null,
                    "first_name": "Thomas",
                    "last_name": "MacKay",
                    "avatar_url": null
                }
            }
        ]
    }

注意“profile”节点而不是“target”节点。

我如何实现这一目标?

2 个答案:

答案 0 :(得分:2)

我认为这也应该有用。在 targets / index.json.rabl

中输入
collection @customers => :targets

child :profile do
  extends 'customers/profile'
end

customers / profile.json.rabl

object @profile
attributes :id, :username, :customer_id, :first_name, :last_name, :avatar_url

答案 1 :(得分:0)

这样的东西应该有用......目前不再使用RABL,但这就是我记忆中的...

node :profile do 
  attributes :id, :username, :customer_id, :first_name, :last_name, :avatar_url
end