删除RABL中的子根节点
我正在尝试使用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" } } ] } ]
我想找到一种方法来删除文件集合中的根“文件”键。 就像是:
[ { "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
尝试更换:
child :files do extends 'groups/_file' end
有:
node :files do |group| group.files.map do |file| partial 'groups/_file', object: file, root: false end end
这是删除根json的常用方法(而不是指定object_root:false)
配置/初始化/ rabl_config.rb
Rabl.configure do |config| config.include_json_root = false end
将它移到那里(并重新启动轨道),修复它?
只是把它放在那里,以防你想为特定的孩子申请:
child :files, :object_root => false do extends 'groups/_file' end