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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top