In a Rails nested form, how does a _destroy checkbox inside fields_for remove a saved child record, and what must be in place?
answer
- a field named [_destroy]
- travels with the row's hidden id
- allow_destroy: true on the parent
- deleted when the parent saves
- _destroy reads marked_for_destruction?
basics
~10 sInside fields_for, f.checkbox :_destroy posts cocktail[ingredients_attributes][0][_destroy] as 1 when ticked, beside the row's hidden id. The parent's accepts_nested_attributes_for needs allow_destroy: true, and the row is deleted only when the parent saves.
solid answer
~40 sInside `f.fields_for :ingredients`, `ingredient_fields.checkbox :_destroy` renders an input named `cocktail[ingredients_attributes][0][_destroy]` with value `1` (plus the helper's usual hidden `0`). The flag only means something next to the row's hidden `[id]`, which `fields_for` adds for persisted children: the id says which saved ingredient, the flag says remove it. On the model, `accepts_nested_attributes_for :ingredients, allow_destroy: true` must be set, otherwise the key is ignored, and the controller must permit `_destroy`. The record is marked for destruction and deleted when the cocktail saves; if the save fails, nothing is deleted and the re-rendered box stays ticked, because Active Record's `_destroy` method returns `marked_for_destruction?`. Removing a saved row's inputs from the page does not delete it; it just leaves that row out of the update.
code
erb · 10 lines<%= f.fields_for :ingredients do |ingredient_fields| %>
<div class="ingredient">
<%= ingredient_fields.text_field :name %>
<%= ingredient_fields.text_field :amount %>
<% if ingredient_fields.object.persisted? %>
<%= ingredient_fields.checkbox :_destroy %>
<%= ingredient_fields.label :_destroy, "Remove" %>
<% end %>
</div>
<% end %>go deeper
Remember that a checkbox named _destroy inside fields_for removes a saved child, and that the parent needs allow_destroy: true.
Explain why the hidden id must travel with the flag, that the delete happens on the parent's save, and why the box stays ticked after a failed save.
Design row removal so saved rows are flagged and new rows dropped, and catch the silent failures: no allow_destroy, or a script that deletes a saved row's inputs.
Weigh in-form deletion against explicit per-row destroy actions when auditing, undo or authorisation rules make a silent bulk removal risky.
## The field itself A nested form lets one cocktail form edit its ingredient rows. To let the user remove a saved ingredient, you add one more field to each child block: ```erb <%= f.fields_for :ingredients do |ingredient_fields| %> <%= ingredient_fields.text_field :name %> <%= ingredient_fields.checkbox :_destroy %> <%= ingredient_fields.label :_destroy, "Remove" %> <% end %> ``` `_destroy` is not a column. Active Record defines a `_destroy` method on every model that returns `marked_for_destruction?`, so the form helper has a value to read, and `accepts_nested_attributes_for` treats a truthy `_destroy` key in a child hash as a removal request. The checkbox is named `cocktail[ingredients_attributes][0][_destroy]` and sends `1` when ticked (the `checkbox` helper also emits its hidden `0` first, so an unticked box posts `0`). ## What must be in place 1. **The hidden id.** `fields_for` adds `[id]` for persisted children. The id identifies which saved ingredient the flag applies to; a hash without an id is a new child, and there is nothing saved to remove. 2. **`allow_destroy: true`.** The parent declares `accepts_nested_attributes_for :ingredients, allow_destroy: true`. Without it the `_destroy` key is silently ignored and the ingredient is kept. 3. **Permitted params.** The controller's strong parameters must include `_destroy` (and `id`) for the child hashes; that is the strong-parameters leaf's ground. 4. **A successful save.** Assignment only marks the child; the delete runs when the cocktail is saved. ## Re-rendering after a failed save If another field fails validation, the action re-renders the form. Two details make that safe: - Nothing was deleted, because the delete only happens as part of a successful save. - The checkbox reads `_destroy` from the child record, and that returns `marked_for_destruction?`, so the user's tick is still shown. They do not have to tick it again. ## Removing rows with JavaScript Many forms replace the checkbox with a "Remove" button. What that button must do depends on the row: | Row | What to do | Why | |---|---|---| | Saved (has a hidden id) | set a hidden `_destroy` field to `1` and hide the row | the server must be told; a missing row is left unchanged | | New (no id yet) | remove its inputs from the DOM | nothing exists to delete; not sending it means it is never built | For the saved case, render `ingredient_fields.hidden_field :_destroy` instead of the checkbox and let the script change its value. The Stimulus controller that does this is the Stimulus leaf's subject; the contract with Rails is only the field name and the value. ## A has_one works the same way For `has_one :garnish` with `accepts_nested_attributes_for :garnish, allow_destroy: true`, the field is `garnish_fields.checkbox :_destroy` and posts `cocktail[garnish_attributes][_destroy]`, beside the garnish's hidden id. There is no index, because there is only one child. ## Why the delete waits for the save Assignment and persistence are separate steps: 1. `@cocktail.update(cocktail_params)` assigns the nested hashes; a flagged ingredient is marked for destruction. 2. Validations run on the cocktail and its children. Marked children are skipped, so a half-filled row the user is deleting does not block the save. 3. The save writes the cocktail and destroys the marked children in the same database transaction, so a later failure rolls the deletes back too. ## Common mistakes - **Deleting the inputs of a saved row.** Nested attributes only touch the children whose hashes arrive. A saved ingredient missing from the params stays in the database. - **Forgetting `allow_destroy: true`.** The box is rendered, ticked and posted, and nothing happens, with no error. - **Dropping the hidden id**, for example with `include_id: false` and no manual `hidden_field :id`. The flag then arrives without an id, so there is no saved record for it to refer to. - **Expecting an immediate delete.** The removal is part of the parent's save, so validation failures elsewhere keep the row. ## Interview summary The form's job is small: render a `_destroy` field next to each saved row's hidden id. The model's `allow_destroy: true` turns that flag into a removal, and the removal happens when the parent saves. Interviewers mostly probe the two silent failures: a missing `allow_destroy` and a row removed from the DOM instead of flagged.
- What happens if the form posts _destroy=1 for a row that has no id?With `allow_destroy: true`, a new child hash flagged for destruction is simply not built, so nothing is created. That is why a script may either delete a new row's inputs or flag them; both end with no record.
- How would you render a Remove button instead of a checkbox?Render `ingredient_fields.hidden_field :_destroy` in each saved row and have a small script set its value to `1` and hide the row. The server sees the same `_destroy` key a ticked checkbox would send.
- Ticking the _destroy box deletes the ingredient even without allow_destroy: true.
- Removing a saved row's inputs from the page deletes that record on save.
- _destroy is a database column you must add with a migration.
- The ingredient is deleted the moment the params are assigned, before save.
- After a failed save the user must tick the _destroy boxes again.