This project is archived and is in readonly mode.

#2392 ✓duplicate
Jeroen van Dijk

to_radio_button_tag ignores :index options partially

Reported by Jeroen van Dijk | April 1st, 2009 @ 02:59 PM | in 2.3.4

When the form_builder method #radio_button is given the :index option it gets ignored for the id of the radio button. The :index option is necessary when using nested forms with new and existing attributes.

The consequence is that you get html elements with duplicate id's and labels won't work with those elements because they do get the index.

The cause is visible in the #to_radio_button:

      def to_radio_button_tag(tag_value, options = {})
        options = DEFAULT_RADIO_OPTIONS.merge(options.stringify_keys)
        options["type"]     = "radio"
        options["value"]    = tag_value
        if options.has_key?("checked")
          cv = options.delete "checked"
          checked = cv == true || cv == "checked"
          checked = self.class.radio_button_checked?(value(object), tag_value)
        options["checked"]  = "checked" if checked
        pretty_tag_value    = tag_value.to_s.gsub(/\s/, "_").gsub(/\W/, "").downcase
        options["id"]     ||= defined?(@auto_index) ?                       ## <--- PROBLEM IS IN HERE!
          "#{tag_id_with_index(@auto_index)}_#{pretty_tag_value}" :
        tag("input", options)

Normally the index option is processed in #add_default_name_and_id but because :id already has been set this is not working.

I dont understand the logic and when I have time I will try to see if I can find it from the additional tests.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>


Referenced by