Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

to_radio_button_tag ignores :index options partially

#2392

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"
        else
          checked = self.class.radio_button_checked?(value(object), tag_value)
        end
        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_id}_#{pretty_tag_value}"
        add_default_name_and_id(options)
        tag("input", options)
      end

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.

Reported by Jeroen van Dijk · April 1st, 2009 @ 02:59 PM

State: duplicate
Milestone: 2.3.4
Assigned to: Eloy Duran Eloy Duran
Importance: High

Activity

  1. José Valim
    José Valim

    Jeroen,

    Just found that too. Here is a patch with tests. If possible, try it and see if it solves for you too.

    April 2nd, 2009 @ 10:26 PM

  2. José Valim
    José Valim
    • Assigned user set to Eloy Duran

    Assigning it to Eloy since this error appears when working with nested attributes.

    July 19th, 2009 @ 01:31 PM

  3. Eloy Duran
    Eloy Duran
    • Milestone changed from 2.x to 2.3.4

    I'm still travelling through Japan, but I will definitely be able to apply this to my 2.3.4 branch. Thanks.

    July 21st, 2009 @ 07:51 AM

  4. José Valim
  5. José Valim
  6. Eloy Duran
    Eloy Duran
    • State changed from new to duplicate

    Thanks for investigating José!

    July 24th, 2009 @ 02:44 AM