Question

I have an API and I want the user to be able to send requests that support nested attributes.

Right now, if a user sends the following JSON, it creates the resource correctly.

{"name": "John", {"pages_attributes": [{"url":"test"}]}}

However, I want to let the user to write pages instead of pages_attributes:

{"name": "John", {"pages": [{"url":"test"}]}}

Anyway to change the nested attributes to do so?

Was it helpful?

Solution

The easiest would be to just modify the hash, like this:

params["pages_attributes"] = params.delete("pages")

You could put that in a before_action to ensure it happens for all the actions in the specific controller.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top