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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -