Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why are 2 keyword arguments being interpreted as 1?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

In the first example the false value is passed explicitly as the checked argument and the disabled variable is interpreted as the options parameter. This means that disabled is treated as a separate argument and gets merged with the default options hash (which is empty in this case).

But in 2nd example the checked: false and disabled: true are passed as a single hash argument to the options parameter. In this case, options includes both the checked and disabled keys.

In the first example, the checked value is explicitly set to false, so it remains false and is not overridden. But in the second example, checked is included as part of the options hash, so it takes the value false from there. To fix the second example:

<%= radio_button_tag 'set_creation_radio', 'set_editor', false, disabled: true %>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading