Rails jbuilder:如何从键等于数组值的数组构建 JSON
Rails jbuilder: how to build a JSON from an array with the key equals to the array values
我是一个非常新的 Rails 开发人员,我正在使用 Jbuilder 通过以下方式构建我的视图:
[:aaa, :bbb, :ccc].each do |value|
json.value do |json| #<------ Here is my error!
json.partial! foo.send(value)
end
end
一切正常,但 json.value
,我的回复如下(显然):
[{
"value" => {...}
"value" => {...}
"value" => {...}
}]
我想要这个:
[{
"aaa" => {...}
"bbb" => {...}
"ccc" => {...}
}]
有什么想法吗?
来自指南:
To define attribute and structure names dynamically, use the set!
method:
json.set! :author do
json.set! :name, 'David'
end
# => "author": { "name": "David" }
解决方法是这样的:
[:aaa, :bbb, :ccc].each do |value|
json.set! value do |json|
json.partial! foo.send(value)
end
end
我是一个非常新的 Rails 开发人员,我正在使用 Jbuilder 通过以下方式构建我的视图:
[:aaa, :bbb, :ccc].each do |value|
json.value do |json| #<------ Here is my error!
json.partial! foo.send(value)
end
end
一切正常,但 json.value
,我的回复如下(显然):
[{
"value" => {...}
"value" => {...}
"value" => {...}
}]
我想要这个:
[{
"aaa" => {...}
"bbb" => {...}
"ccc" => {...}
}]
有什么想法吗?
来自指南:
To define attribute and structure names dynamically, use the
set!
method:json.set! :author do json.set! :name, 'David' end # => "author": { "name": "David" }
解决方法是这样的:
[:aaa, :bbb, :ccc].each do |value|
json.set! value do |json|
json.partial! foo.send(value)
end
end