This project is archived and is in readonly mode.

#3416 ✓duplicate
yanosz

fields_for renders invalid html

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

Hello,

this code:

        <table class="list" id="company_addresses" width="100%">
            <thead>
                <tr>
                    <th>City</th>
                    <th>Zip</th>
                    <th>Street</th>
                    <th>Country</th>
                    <th>Info</th>
                    <th>Last change</th>
                </tr>
            </thead>
            <%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%">
<thead>
</thead>
<input id="company_company_addresses_attributes_1_id" type="hidden" value="1" name="company[company_addresses_attributes][1][id]"/>
<tbody>
<tr class="contact_company_address show address list-line-odd">
</tr>
<tr class="edit address contact_company_address" style="display: none;">
</tr>
<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
imho:
* 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)
      else
        @template.fields_for(name, object, *args) do |builder|
          @template.concat builder.hidden_field(:id) 
          block.call(builder)
        end
      end
    end</code>



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

Keep smiling
yanosz

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="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages