I solved this using content_for. I put everything in the first
fields_for but the fields I wanted to move to a different partial, I
placed in the content_for block. See the example below.

Hope this helps,

Matt

# example
  <%= f.fields_for :cars do |car_fields| %>
    <div class="field">
      <%= car_fields.label :color %><br />
      <%= car_fields.text_field :color %><br />
    </div>
    <% content_for :plate_fields do %>
      <div class="field">
        <%= car_fields.label :plate %><br />
        <%= car_fields.text_field :plate %><br />
      </div>
    <% end %>
  <% end %>

  =======
  <%# This can go anywhere... i.e. another partial %>
  <%= content_for :plate_fields %>


On Aug 17, 11:40 pm, Matt Smith <matthewcalebsm...@gmail.com> wrote:
> With the following code and result below, why does the cars_attributes
> index (i.e. name=owner[cars_attributes][>>>0<<<][color]) increment in
> the second fields_for block when only one car is built? Shouldn't it
> stay the same because only one car was built?
>
> Is this standard behavior? If so, what is the standard way to force
> both indexes to be 0 (or increment together if there are more
> records)?
>
> This is an over-simplified example. The reason I want two fields_for
> blocks is due the arrangement of partials in another complex form.
>
> Much thanks!
>
> Matt
>
> # owners_controller.rb
> # ...
>   def new
>     @owner = Owner.new
>     @owner.cars.build
>
>     respond_to do |format|
>       format.html # new.html.erb
>       format.xml  { render :xml => @owner }
>     end
>   end
> # ...
>
> # owner.rb
> class Owner < ActiveRecord::Base
>   has_many :cars
>   accepts_nested_attributes_for :cars
> end
>
> # car.rb
> class Car < ActiveRecord::Base
>   belongs_to :owner
> end
>
> # new.html.erb
> <%= form_for(@owner) do |f| %>
>   <% if @owner.errors.any? %>
>     <div id="error_explanation">
>       <h2><%= pluralize(@owner.errors.count, "error") %> prohibited
> this owner from being saved:</h2>
>
>       <ul>
>       <% @owner.errors.full_messages.each do |msg| %>
>         <li><%= msg %></li>
>       <% end %>
>       </ul>
>     </div>
>   <% end %>
>
>   <div class="field">
>     <%= f.label :name %><br />
>     <%= f.text_field :name %>
>   </div>
>
>   <h2>Cars</h2>
>
>   <%= f.fields_for :cars do |car_fields| %>
>     <div class="field">
>       <%= car_fields.label :color %><br />
>       <%= car_fields.text_field :color %>
>     </div>
>   <% end %>
>
>   =======
>
>   <%= f.fields_for :cars do |car_fields| %>
>     <div class="field">
>       <%= car_fields.label :plate %><br />
>       <%= car_fields.text_field :plate %>
>     </div>
>   <% end %>
>
>   <div class="actions">
>     <%= f.submit %>
>   </div>
> <% end %>
>
> #http://localhost:8081/owners/new
> # ...
>   <h2>Cars</h2>
>
>     <div class="field">
>       <label for="owner_cars_attributes_0_color">Color</label><br />
>       <input id="owner_cars_attributes_0_color"
> name="owner[cars_attributes][0][color]" size="30" type="text" />
>     </div>
>
>   =======
>
>     <div class="field">
>       <label for="owner_cars_attributes_1_plate">Plate</label><br />
>       <input id="owner_cars_attributes_1_plate"
> name="owner[cars_attributes][1][plate]" size="30" type="text" />
>     </div>
> # ...

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to