Label's for attribute doesn't match the id when nil is given in collection

Author: masasakanoCreated Apr 21, 2024Updated Apr 19, 2026

Environment

  • Ruby 3.1.2p20
  • Rails 7.0.8
  • Simple Form 5.3.0

Current behavior

When a collection is explicitly given to radio-buttons and one of the values is nil, the generated for label for the nil one does not match its corresponding id with a difference of a single underscore character. For example,

erb
<%= form.input :my_method, as: :radio_buttons, label: "ABC",
      collection: [['Yes', true], ['Undefined', nil]], checked: nil %>

for Model generates

xml
<fieldset class="form-group radio_buttons model_my_method">
<legend class="col-form-label pt-0">ABC</legend>
<input type="hidden" name="model[my_method]" value="" autocomplete="off" />
<!-- Valid one for "true" -->
<div class="form-check">
  <input class="form-check-input radio_buttons required" type="radio" value="true" name="model[my_method]"
    id="model_my_method_true" />
  <label class="form-check-label collection_radio_buttons"
    for="model_my_method_true">Yes</label>
</div>

<!-- Invalid one for "nil" -->
<div class="form-check">
  <input class="form-check-input radio_buttons required" type="radio" checked="checked" name="model[my_method]"
    id="model_my_method" />
  <label class="form-check-label collection_radio_buttons"
    for="model_my_method_">Undefined</label>
</div>

The second pair of input and label corresponds to the nil-parameter. As you see, the for has a trailing _, whereas that for id does not, and so they are not consistent.

Expected behavior

The label and corresponding ID values must exactly agree.