HeadlinesBriefing favicon HeadlinesBriefing.com

Rails Nested Forms Without Nested Attributes

DEV Community •
×

Rails developers often rely on accepts_nested_attributes_for to build complex forms, but a recent post on Rails Designer shows a lighter alternative. By treating each field as its own model and rendering separate forms, the author sidesteps nested attributes entirely, simplifying validation and routing while keeping URLs tidy today.

Under the hood, the solution uses Single Table Inheritance to distinguish field types like TextField and TextArea, and overrides `model_name` to produce clean paths such as /forms/1/fields. Each field gets its own form_with block, so updates are handled independently without complex params parsing for every field instance today.

Auto‑save is powered by the author’s new Attractive.js library, which listens for a pause in typing and submits the form after 1.5 seconds. This eliminates the need for per‑field submit buttons, keeping the UI clean and making the approach feel almost invisible to the end user in production.

Because each form operates independently, the parent form saves the form record while child forms persist their own field records. No nested attributes, no heavy controller logic, and no manual strong‑parameter juggling. The result is a maintainable, testable pattern that scales well for SaaS form builders and applications.