【问题标题】:rabl wrap collection in json objectjson 对象中的 rabl 包装集合
【发布时间】:2012-10-20 20:07:18
【问题描述】:

我正在使用 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 对象,其中包含 media 数组。我有 rabl 配置设置,例如:

   config.include_json_root = false
   config.include_child_root = false

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

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

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

感谢任何帮助

【问题讨论】:

    标签: ruby-on-rails json rabl


    【解决方案1】:

    我已经设法这样做了:

    collection @articles, :root => "articles", :object_root => false
    attributes *Article.column_names
    
    child(:media) do
      attributes *Medium.column_names
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多