9

This question answered my question partly. The author uses a similar json structure..

My Question: How to permit nested arrays in a nested object? I have a Contribution model with has_many Features. I am trying to create GeoJSON polygons. The coordinates stays empty

This is the JSON I am sending

{
  "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"
  }
}

Currently my permit code looks like this:

params.require(:contribution).permit(
  :title,
  :description,
  features_attributes: [
    { geojson: [
        :type,
        { geometry: [
            :type,
            #{ coordinates: [] } # this works for arrays like coordinates: [ 7.62, 51.96 ]
            { coordinates: [[]] }
          ]
        }
      ]
    }
  ]
)
Community
  • 1
  • 1
ubergesundheit
  • 544
  • 1
  • 4
  • 13
  • Why are you using a hash for `geojson`? Surely you'd use `features_attributes: [ geojson:[` instead of `features_attributes: [ {}` – Richard Peck Apr 27 '14 at 09:34
  • I tested your suggestion, It makes no difference. I think ruby recognizes that `geojson` is a hash. The curly braces are just for me :) – ubergesundheit Apr 27 '14 at 11:33
  • Yikes! I grant that the accepted answer works. Yet it seems like a workaround. Does anyone know if the core API supports this functionality in any way? – Jeff Gandt Oct 16 '15 at 15:08

1 Answers1

9

I solved it now like this. Please correct me! :)

  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
ubergesundheit
  • 544
  • 1
  • 4
  • 13