Permitting array of hashes rails 5

Viewed 3020

My rails version is 5 and I have request param like this,

{ "segment": {
    "name": "test",
    "new_filters": [
      {"criteria": "sad",
        "other_keys": [{"key": "value"}]
      }, 
      {"criteria": "sad",
        "other_keys1": [{"key1": "value1"}]
      }]
}
}

I am stuck in permitting the new_filter params in rails controller, I am trying below code,

params.require(:segment).permit(:name, :people_count, new_filters: [])

and still getting the error. But this is not the case while having array of strings in new_filter key. Eg: ["sad", "asdasd"]. How to get the nested structure as whitelisted attribute?

2 Answers

This worked for me when testing with your attributes:

params.require(:segment).permit(:name, :people_count, new_filters: [:criteria, other_keys: [:key], other_keys1: [:key1]])
Related