0

我正在使用 RABL 生成 JSON 响应。我无法实现以下结构:

{
  "articles": [
     {
        "created_at": "2012-10-20T15:57:31Z",
        "description": "MK DEsc",
        "id": 1,
        "title": "MK Title",
        "updated_at": "2012-10-20T15:58:14Z",
         media:[{
            id : 1,
            title : "title"
        }]
     },
     {
        "created_at": "2012-10-20T16:38:24Z",
        "description": "fdsfdffffffff",
        "id": 2,
        "title": "asdads",
        "updated_at": "2012-10-20T16:38:24Z",
         media:[{
            id : 1,
            title : "title"
        }]
     }
   ]
}

我有一个包含媒体数组的@artices对象。我有 rabl 配置设置,例如:

   config.include_json_root = false
   config.include_child_root = false

到目前为止,我的 .rabl 模板如下所示:

object false
node :articles do
  @articles.each do |article|
  end
end

我无法在此结构中添加媒体信息。

任何帮助表示赞赏

4

1 回答 1

2

我已经设法这样做了:

collection @articles, :root => "articles", :object_root => false
attributes *Article.column_names

child(:media) do
  attributes *Medium.column_names
end
于 2012-10-20T20:12:32.820 回答