Why does the following work to correct disable a radio button in a form:
<%= radio_button_tag 'set_creation_radio', 'set_editor', false, disabled: true %>
But this does not (note addition of keyword argument checked
):
<%= radio_button_tag 'set_creation_radio', 'set_editor', checked: false, disabled: true %>
When using a breakpoint at this location, checked
produces the following output in the first (i.e. working) instance:
0> checked
=> true
whereas the second seems to interpret both keywords as a single argument:
0> checked
=> {:checked=>true, :disabled=>true}
This is the source code for radio_button_tag
and I do not understand why checked
would be interpreted as a hash in my example:
def radio_button_tag(name, value, checked = false, options = {})
html_options = { "type" => "radio", "name" => name, "id" => "#{sanitize_to_id(name)}_#{sanitize_to_id(value)}", "value" => value }.update(options.stringify_keys)
html_options["checked"] = "checked" if checked
tag :input, html_options
end
There's no magic in calling a method in Ruby that says "oh, that's a hash, so I'll move that to the fourth argument". So in your second example you have three arguments:
'set_creation_radio'
'set_editor'
{ checked: false, disabled: true }
So those are assigned to the first three parameters in the radio_button_tag
method call, ie.
name = 'set_creation_radio'
value = 'set_editor'
checked = { checked: false, disabled: true }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With