gpt4 book ai didi

ruby-on-rails - 强参数中嵌套对象中的Rails 4嵌套数组

转载 作者:行者123 更新时间:2023-12-04 07:22:39 25 4
gpt4 key购买 nike

This问题部分回答了我的问题。作者使用类似的json结构。

我的问题:如何在嵌套对象中允许嵌套数组?我有一个带有Contributionhas_many模型Features。我正在尝试创建GeoJSON多边形。coordinates保持为空

这是我要发送的JSON

{
"contribution": {
"features_attributes": [
{
"geojson": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
7.263336181640625,
52.07190953840937
],
[
7.263336181640625,
52.135173926548894
],
[
7.404785156249999,
52.135173926548894
],
[
7.404785156249999,
52.07190953840937
],
[
7.263336181640625,
52.07190953840937
]
]
]
}
}
}
],
"title": "324",
"description": "23"
}
}

目前,我的许可代码如下所示:
params.require(:contribution).permit(
:title,
:description,
features_attributes: [
{ geojson: [
:type,
{ geometry: [
:type,
#{ coordinates: [] } # this works for arrays like coordinates: [ 7.62, 51.96 ]
{ coordinates: [[]] }
]
}
]
}
]
)

最佳答案

我现在这样解决了。请纠正我! :)

  params.require(:contribution).permit(
:title,
:description,
features_attributes: [
{
geojson: [
:type,
{ geometry: [
:type,
{ coordinates: [] },
coordinates: []
]
}
]
}
]
).tap do |whitelisted|
whitelisted['features_attributes'].try(:each_index) do |i|
whitelisted['features_attributes'][i]['geojson']['geometry']['coordinates'] = params['contribution']['features_attributes'][i]['geojson']['geometry']['coordinates']
end
end

关于ruby-on-rails - 强参数中嵌套对象中的Rails 4嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23316384/

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