Is there a reason why collection_radio_buttons doesn't have an include_blank option?

Is there a reason why collection_radio_buttons doesn’t have an option for “include_blank” like collection_select does? I’ve found where there is an “include_hidden” option, but what if I want the user to have the option of not selecting any option from my collection I have passed? Or want them to have the ability to change the associated record to nil in my form?

I’ve never contributed to Rails so I was thinking perhaps I’ll make an attempt with adding this option, but wasn’t sure if there was a reason for it not being there in the first place.

1 Like

It could be because the native form element doesn’t have any way to “unselect” an option once chosen.

You can choose a different option within the same-name element group, and you’d have to create that option as a deliberate blank value input. So if you added another option to the elements, it would need to be <input type="radio" id="foo_bar_" value="" name="foo[bar]"> <label for="foo_bar_">None</label>.

Since Rails introspects the options available within the collection, and this is an entirely made-up option, it would have to create that option from the parameters given to the helper. So maybe the thing to do would be to map this to allow_blank: 'None' or something like that.

Another way to get this going would be to manually insert the null option at the beginning or end of the collection you pass to collection_radio_buttons, maybe something like collection_radio_buttons(:color, Array( 'None')) + Color.all.to_a, :name, :id).