0

我找不到解决方案,所以我决定在这里问。我有这个数组(如果需要可以更改结构):

[[{:time=>2014, :amount=>148.24018480434023}, {:time=>2015, :amount=>152.5625744766238}], [{:time=>2014, :amount=>151.06717076828187}, {:time=>2015, :amount=>158.43869963240266}]]

在 Rabl 模板中,我有:

collection @array, object_root: false

node :collection do |s|
  s 
end

回应是:

[{"collection": [{"time": 2014,"amount": 148.24018480434023},{"time": 2015,"amount": 152.5625744766238}]},{"collection": [{"time": 2014,"amount": 151.06717076828187},{"time": 2015,"amount": 158.43869963240266}]}]

但是我想摆脱“收集”节点并得到这个响应:

[ [{ time: 2014, amount: 138.92 }, { time: 2015, amount: 142.98 }], [{ time: 2014, amount: 141.57 }, { time: 2015, amount: 148.48 }] ]
4

2 回答 2

3

如果你想摆脱根尝试

#  config/initializers/rabl_init.rb
Rabl.configure do |config|
  config.include_json_root = false
  config.include_child_root = false
end

https://github.com/nesquena/rabl/issues/4

类似:删除 RABL 中的子根节点

于 2013-07-02T11:00:55.213 回答
1

我知道这是一个老问题,但为了后代。最简单的方法是删除节点调用上的 :collection。因此,对于您的示例,如下所示

collection @array, object_root: false

node do |s|
  s 
end
于 2014-05-10T08:30:55.077 回答