Defect #28447

fields_for produces a broken <label for=“”> value

Added by Phlip Plumlee over 4 years ago.

Status:NewStart date:
Priority:NormalDue date:
Assignee:-% Done:


Target version:-
Resolution: Affected version:3.4.2


Here's some code from the redmine_people plugin illustrating the problem:

<%= f.fields_for :information do |information| >
= information.text_field :middlename, :label => l(:label_people_middlename) >&lt;/p&gt;
= f.text_field :lastname, :required => true %>&lt;/p&gt;

Then one of the fields declares a label:

&lt;p&gt;<%= :gender, Person.get_genders, :label => l(:label_people_gender)%>&lt;/p&gt;

That generates this HTML:

&lt;p&gt;&lt;label for="person[information_attributes]_gender"&gt;Gender&lt;/label&gt;
&lt;select name="person[information_attributes][gender]" id="person_information_attributes_gender"&gt;
&lt;option selected="selected" value="0"&gt;Male&lt;/option&gt;
&lt;option value="1"&gt;Female&lt;/option&gt;

The <label for=""> value has [] in it instead of underscores _, so it does not match the target field's id="person_information_attributes_gender". Clicking on the label does not put the keyboard focus into that <select> field.

The bug comes from this statement in LabelledFormBuilder:

@template.content_tag("label", text.html_safe,
:class => (@object && @object.errors[field].present? ? "error" : nil),
:for => (@object_name.to_s + "_" + field.to_s))

The object_name has [] in it, so something should replace them with "_".

Also available in: Atom PDF