gpt4 book ai didi

ruby-on-rails - rails jbuilder : how to build a JSON from an array with the key equals to the array values

转载 作者:行者123 更新时间:2023-12-04 14:32:17 24 4
gpt4 key购买 nike


我是一个非常新的 Rails 开发人员,我正在使用 Jbuilder按照以下方式构建我的 View :

[: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

关于ruby-on-rails - rails jbuilder : how to build a JSON from an array with the key equals to the array values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30939844/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com