While this is mostly straight forward, I had to wrangle deleting shifts in the
form on an already existing tracker. If you set _destroy: true on a nested
attribute, it will be destroyed, but only after the parent model is saved.
That's not great for a form that's being edited but not yet saved.
So instead, I replaced the <button> with a checkbox styled to look like a
button. The label is hidden visually but still there for screen readers. If the
checkbox is checked, css hides the whole field! This works with the css :has
selector. So now the nested object persists on the form so it can be eventually
removed, or reverted when canceled, but appears removed for the user when
editing.
To make it not show up on the form, you have to:
assign_attributes to the already persisted model after fetching in the
controller
- check in nested form to see if the object it
marked_for_destruction?. If it
is, hide everything but the checkbox holding the _destroy attribute.
assign_attributes converts the _destroy params into mark_for_destruction
calls on the model.
Screen Shot 2026年07月08日 at 16.42.44-fullpage
While this is mostly straight forward, I had to wrangle deleting shifts in the
form on an already existing tracker. If you set `_destroy: true` on a nested
attribute, it will be destroyed, but only after the parent model is saved.
That's not great for a form that's being edited but not yet saved.
So instead, I replaced the `<button>` with a checkbox styled to look like a
button. The label is hidden visually but still there for screen readers. If the
checkbox is checked, css hides the whole field! This works with the css `:has`
selector. So now the nested object persists on the form so it can be eventually
removed, or reverted when canceled, but _appears_ removed for the user when
editing.
To make it not show up on the form, you have to:
- `assign_attributes` to the already persisted model after fetching in the
controller
- check in nested form to see if the object it `marked_for_destruction?`. If it
is, hide everything but the checkbox holding the `_destroy` attribute.
`assign_attributes` converts the `_destroy` params into `mark_for_destruction`
calls on the model.
