Rabl和数组数组

时间:2013-06-19 15:13:50

标签: ruby-on-rails ruby rabl

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

[[{: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 }] ]

2 个答案:

答案 0 :(得分: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

类似:Removing child root nodes in RABL

答案 1 :(得分:1)

我知道这是一个古老的问题但是为了后人的缘故。最简单的方法是删除节点调用上的:collection。所以对于你的例子,它将如下所示

collection @array, object_root: false

node do |s|
  s 
end