This project is archived and is in readonly mode.

#3416 ✓duplicate

fields_for renders invalid html

Reported by yanosz | October 23rd, 2009 @ 11:40 AM


this code:

        <table class="list" id="company_addresses" width="100%">
                    <th>Last change</th>
            <%form.fields_for :company_addresses do |company_address_field|%>
                <%= render :partial => 'address/address_mapping', :object => company_address_field%>
            <% end %>

renders as:

<table id="company_addresses" class="list" width="100%">
<input id="company_company_addresses_attributes_1_id" type="hidden" value="1" name="company[company_addresses_attributes][1][id]"/>
<tr class="contact_company_address show address list-line-odd">
<tr class="edit address contact_company_address" style="display: none;">
<input id="company_company_addresses_attributes_2_id" type="hidden" value="2" name="company[company_addresses_attributes][2][id]"/>
<tr class="contact_company_address show address list-line-even">
<td> 123 </td>
<td class="zip"> 123 </td>
<td> 123 </td>

while address_mapping is a partial rendering address forms. As seen above, input's are render within various html elements resulting in invalid html-Code
* fields_for should not write anything to the dom, if included with <% %> and not <%= %>

This is caused by action_view/helpers/form_helper.rb 1030:

        def fields_for_nested_model(name, object, args, block)

      if object.new_record?
        @template.fields_for(name, object, *args, &amp;block)
        @template.fields_for(name, object, *args) do |builder|
          @template.concat builder.hidden_field(:id) 


Imho fields_for_nested_model should not insert id-fields and fields_for in general should not depend on inserted ids.

Keep smiling

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>