Suppose I have a simple to-do list application. The application contains two models:
- lists (have an owner and description)
- items (have name and due-date) that belong to a specific list
I would like to have a single edit screen for a list in which I update the list attributes (such as description) and also create/delete/modify associated items. There should be a single “save” button that will commit all changes. Unless save is pressed, any change to the list and the items should be forgotten.
I wasn’t able to find an elegant best practice for this. Would greatly appreciate any suggestions and/or references to existing implementations.
You should be able to make this work with
accepts_nested_attributes_foron thehas_manyassociation. Quoting from the Rails API docs:There’s also a good explanation in Railscast 196 which shows how to set up forms with nested attributes.