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: [[]] }
          ]
        }
      ]
    }
  ]
)
3
  • Why are you using a hash for geojson? Surely you'd use features_attributes: [ geojson:[ instead of features_attributes: [ {} Commented Apr 27, 2014 at 9: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 :) Commented Apr 27, 2014 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? Commented Oct 16, 2015 at 15:08

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.