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.

fields_for renders invalid html

#3416

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

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

State: duplicate
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Arnau Sanchez
    Arnau Sanchez

    Same problem here, a fields_for inside a table ends with invalid HTML code. I was able to solve it thanks to:

    http://stackoverflow.com/questions/1236416/ruby-on-rails-fieldfor-f...

    But I agree with yanosz, there should be an option to tweak how the hidden field is inserted.

    February 26th, 2010 @ 07:05 PM

  2. Tyler Rick
  3. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from new to duplicate

    March 30th, 2010 @ 07:38 PM