我正在尝试使用RABL渲染一个非常简单的数据结构,但是我不知道如何正确删除子根节点。这是我的两个模板。

首先,收集索引模板。

collection @groups, :object_root => false

attributes :id, :name
child :files do
  extends 'groups/_file'
end

接下来是文件部分模板。
object @file

attributes :id

这两个模板最终产生以下JSON:
[
   {
      "id":"4f57bf67f85544e620000001",
      "name":"Some Group",
      "files":[
         {
            "file":{
               "id":"4f5aa3fef855441009000007"
            }
         }
      ]
   }
]

我想找到一种方法来删除文件集合中的根"file"键。就像是:
[
   {
      "id":"4f57bf67f85544e620000001",
      "name":"Some Group",
      "files":[
         {
            "id":"4f5aa3fef855441009000007"
         }
      ]
   }
]

最佳答案

在最新版本的Rabl上,如果您希望include_root_json在所有级别上都是false,则必须设置此配置。

Rabl.configure do |config|
  config.include_json_root = false
  config.include_child_root = false
end

09-10 16:06
查看更多