Rabl, remove parent element of children

喜欢而已 提交于 2019-12-11 01:02:00

问题


could anyone help me get rid of the "item" element within "items

the output should be:
"list":{ "items":[ { { data1: "test" } , { data2: "test2" } } }

collection @list

child :items => :items do
  attributes :place_id
end

child :items do
  child :place do |place|
    extends 'places/place', object: place
  end
end

current output:
"list":{ "items":[{ "item":{data1: "test"} "item":{ data2: "test2"}}}


回答1:


I had the same problem and this was the solution. You may need to create the file named to configure rabl.
config/initializers/rabl.config.rb

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



回答2:


Old question, for future users' reference:

Fire up the file rabl_init.rb, turn off root by config.include_json_root = false, if you still want a root in your collection objects, use something like:

collection @orders, :object_root=>"order"
    extends "api/v1/orders/show"
# End of @orders



回答3:


There's a working solution for removing the root elements in child nodes at: Removing child root nodes in RABL

Setting the config.include_json_root = false seems to remove only the first level root of the JSON.



来源:https://stackoverflow.com/questions/10109963/rabl-remove-parent-element-of-children

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!