0

New to rabl and not sure how to do this with two different arrays returned in a single hash like this:

@data={:locations => [location1, location2], :items => [item1,item2]}

In my rabl file, I'd like to do something like the following:

@data[:locations]
extends "api/location_show"
@data[:items]
extends "api/item_show"

to output this:

{
    "locations": [
        {
            "id": 156,
            "name": "Location 1"
        },
        {
            "id": 158,
            "name": "Location 2"
        }
    ],
    "items": [
        {
            "global_id": 3189,
            "header": "pistachio 1"
        },
        {
            "global_id": 3189,
            "header": "pistachio 2"
        }
    ]
}

but it just doesn't seem to be working. Is there a way to get this to work?

thx

4

1 回答 1

1

您的 rabl 文件应类似于:

object false
child (:locations) { attributes :id, :name }
child (:items) { attributes :global_id, :header }

通过将 object 设置为 false,您实际上是在告诉 rabl 您想自己构建节点。然后,您可以继续并根据需要调用子方法和节点方法。

于 2012-06-26T18:51:26.040 回答