Rails jbuilder: how to build a JSON from an array with the key equals to the array values -
i'm pretty new rails developer, i'm using jbuilder build view in following way:
[:aaa, :bbb, :ccc].each |value| json.value |json| #<------ here error! json.partial! foo.send(value) end end
everything works json.value
, response following (obviously):
[{ "value" => {...} "value" => {...} "value" => {...} }]
i'd have 1 instead:
[{ "aaa" => {...} "bbb" => {...} "ccc" => {...} }]
any ideas?
from guide:
to define attribute , structure names dynamically, use
set!
method:json.set! :author json.set! :name, 'david' end # => "author": { "name": "david" }
the solution so:
[:aaa, :bbb, :ccc].each |value| json.set! value |json| json.partial! foo.send(value) end end
Comments
Post a Comment