skip to content

In a Rails registration form, which field helpers add hidden inputs of their own, and how can those values corrupt saved array data?

level: seniorimportance: should knowfreq 28%

answer

  1. every helper that can send nothing
  2. checkbox 0, the others an empty string
  3. radio_button adds none
  4. ["", "vegan"] in an array column
  5. include_hidden: false and compact_blank

basics

~20 s

checkbox, select with multiple: true, collection_checkboxes, collection_radio_buttons and multiple file_field add hidden inputs so an empty choice still submits; radio_button does not. In array params those values arrive as "0" or "" and can be saved unless stripped.

solid answer

~40 s

Several helpers add a hidden companion so that "nothing chosen" still reaches the server. `checkbox` writes the unchecked value, `"0"`; a `multiple: true` select, `collection_checkboxes`, `collection_radio_buttons` and a `multiple: true` file field write an empty string, the last because new apps' defaults turn `multiple_file_field_include_hidden` on. A lone `radio_button` writes none, so no choice means no key. In array names the companion becomes an element: a multi-select of dietary needs arrives as `["", "vegan"]`. Association id writers such as `workshop_ids=` drop blanks themselves, but a JSON or array column stores the `""`, and `checkbox ... multiple: true` with the default unchecked value adds `"0"`s. Strip blanks with `compact_blank` before saving, pass `nil` as the checkbox's unchecked value, or use `include_hidden: false` when an absent key is acceptable.

code

erb · 7 lines
erb
<%= form_with model: @registration do |f| %>
  <%= f.select :dietary_needs,
        [["Vegetarian", "vegetarian"], ["Vegan", "vegan"], ["Gluten free", "gluten_free"]],
        {}, { multiple: true } %>

  <%= f.collection_checkboxes :workshop_ids, Workshop.all, :id, :title %>
<% end %>

go deeper

for a junior

Recall that some helpers add hidden inputs so an empty choice still submits, and that radio_button alone does not.

for a middle

Explain last-value-wins for plain keys versus appending for [] keys, and list which helpers write "0" and which write an empty string.

for a senior

Diagnose phantom entries in JSON or array columns, know that association id writers clean blanks themselves, and choose when include_hidden: false is safe.

for a principal

Set a team convention for array-valued attributes, deciding where blank stripping lives so every form and API path stores clean data.

## Why helpers add hidden inputs Some HTML controls submit **nothing** when the user chooses nothing: an unticked checkbox, a multi-select with every option deselected, a group of radio buttons with none picked, a multiple file input with no files. On an edit form, "nothing" means "no key in params", and `update` leaves the old value in place. Rails' helpers fix that by writing a **hidden companion** with the same name, so a value always arrives. ## Which helpers do it | Helper | Hidden companion | Value | Turn it off | |---|---|---|---| | `f.checkbox :newsletter` | before the box | `"0"` (the unchecked value) | `include_hidden: false` or a `nil` unchecked value | | `f.checkbox :dietary_needs, { multiple: true }, "vegan"` | one per box, name ends in `[]` | `"0"` | pass `nil` as the unchecked value | | `f.select :dietary_needs, ..., {}, { multiple: true }` | one, name ends in `[]` | `""` | `include_hidden: false` | | `f.collection_checkboxes` | one, name ends in `[]` | `""` | `include_hidden: false` | | `f.collection_radio_buttons` | one, plain name | `""` | `include_hidden: false` | | `f.file_field :documents, multiple: true` | one, name ends in `[]` | `""` | `include_hidden: false` | | `f.radio_button` | none | none | not applicable | The multiple file field depends on configuration: `multiple_file_field_include_hidden` is set to true by `load_defaults` 7.0 and later, so a new 8.1 app gets the hidden input while an app on older defaults does not. ## How the values reach the model For a plain key, a later value overwrites an earlier one, so a ticked checkbox's `"1"` replaces its hidden `"0"`. For a key ending in `[]`, every value is **appended**, so the companion becomes an element of the array: - a multi-select with "vegan" chosen arrives as `["", "vegan"]`; - the same select with nothing chosen arrives as `[""]`; - three dietary checkboxes using `multiple: true` and the default unchecked value, with only "vegan" ticked, arrive as `["0", "0", "vegan", "0"]`: every box writes its hidden `"0"` just before itself, ticked or not. ## Where it corrupts data and where it does not 1. **Association id writers are safe.** `registration.workshop_ids = ["", "4"]` drops blank entries itself before loading records, so a `has_many :through` checkbox group or multi-select needs no cleanup. 2. **Array and JSON columns are not.** An attribute such as `dietary_needs` stored as JSON keeps `""` or `"0"` exactly as assigned, and later code that counts dietary needs or renders badges sees phantom entries. 3. **Custom setters** receive the raw array and must decide what blank means. The usual fix is to strip blanks where the value enters the model, for example a setter that assigns `Array(value).compact_blank`, while keeping the hidden input so that "clear all" still sends the key. ## Choosing whether to keep the companion - Keep it on edit forms where clearing a value must be possible; removing it brings back the "cannot uncheck" bug. - Remove it with `include_hidden: false` on filter or search forms, where an absent key simply means "no filter" and a blank entry would only pollute the query. - For checkbox arrays, pass `nil` as the unchecked value and rely on a single hidden `""`, or switch to `collection_checkboxes`, which writes exactly one companion for the group. - A `disabled:` checkbox copies `disabled` to its hidden input, so neither is submitted; a disabled field never clears the stored value. ## Debugging checklist When an array attribute holds unexpected `""` or `"0"` entries: 1. Inspect the rendered HTML for hidden inputs that share the field's name, and note whether the name ends in `[]`. 2. Read the request's parameters in the server log to see the array exactly as it arrived. 3. Check whether the attribute is an association id writer, which cleans blanks itself, or a column that stores what it is given. 4. Decide where cleanup belongs: in the model's writer, so every path into the attribute is covered, rather than in one controller.

  • Why does a lone set of radio_button calls behave differently from collection_radio_buttons when nothing is chosen?
    `radio_button` writes only the radio input, so with no choice the key is absent and `update` keeps the old value. `collection_radio_buttons` adds one hidden input with an empty value under the same name, so the key arrives as `""`, which a model can treat as clearing the attribute.
  • When is include_hidden: false the right call on a multi-select?
    On filter or search forms, where an absent key means no filter and the blank entry would only add an empty condition. On edit forms keep the hidden input; without it, deselecting every option sends no key, and the record keeps its old values.

  • radio_button adds a hidden empty value so an unanswered group still submits.
  • A multi-select's blank entry is always harmless because Rails strips it from params.
  • workshop_ids= saves a blank association row when it receives ["", "4"].
  • Removing the hidden input with include_hidden: false has no effect on edit forms.
  • A disabled checkbox still submits its hidden 0 and clears the stored value.

Assumes Rails 8.1 with load_defaults 8.1; the multiple file field's hidden empty input comes from multiple_file_field_include_hidden, which load_defaults 7.0 and later turn on.