I want my JavaScript front-end to pass a recursive data structure to my Rails API.
How do I configure the parameter to permit to handle a structure of unknown depth?
Specifically, my data structure describes a hierarchy of categories. For example:
- Category 1
- Category 1a
- Category 1b
- Category 1b1
- Category 1b2
- Category 1c
- Category 2
- Category 2a
- Category 3
The resulting JSON sent to the API will be an array of objects of the form {name: "Category 1", subcategories: [ ...]}. Where the subcategories object is also an array of { name: 'Foo', subcategories: [...]} For example:
[{"name":"Teaching","subcategories":[{"name":"MTH 201","subcategories":[{"name":"Lecture"},{"name":"Prep"},{"name":"Grading"},{"name":"Office Hours","subcategories":[{"name":"In Person"},{"name":"Zoom"}]}]},{"name":"MTH 202","subcategories":[{"name":"Lecture"},{"name":"Prep"},{"name":"Grading"},{"name":"Office Hours","subcategories":[{"name":"In Person"},{"name":"Zoom"}]}]}]},{"name":"Scholarship"},{"name":"Service","subcategories":[{"name":"Meetings"},{"name":"Paperwork"}]}]
I was able to successfully hard-code my depth to 3 like this:
permit(:name, subcategories: [:name, subcategories: [:name]]).to_h
But, three is not deep enough. Is there a way to either
Allow a specific maximum depth (say, 8), without having to build a massive, recursive description like the one above, or
Allow an input of any depth?
There's no way to do that with the Rails helper, you'll need to recurse through the structure yourself checking that only the keys you expect are there.