views:

438

answers:

1

I'm trying to build two models from one form by using the fields_for method. My code looks like this:

<% for scale in @event.scales %>
  <% f.fields_for "scale[]", scale do |scale_form| %>
    <p>
      Scale<br />
      <%= scale_form.label :name %>
      <%= scale_form.text_field :name %>
      <%= scale_form.label :price %>
      <%= scale_form.text_field :price %>
    </p>
  <% end %>
<% end %>

but unfortunately the output html is missing the IDs of the scales:

<p>
  Scale<br />
  <label for="event_scale__name">Name</label>
  <input id="event_scale__name" name="event[scale][][name]" size="30" type="text" />
  <label for="event_scale__price">Price</label>
  <input id="event_scale__price" name="event[scale][][price]" size="30" type="text" />
</p>

...

Here's the corresponding controller and model:

class EventsController < ApplicationController
  ...

  def new
    @event = Event.new
    @providers = Provider.find(:all, :order => :name)
    3.times { @event.scales.build }
    respond_to do |format|
      format.html
    end
  end

  ...
end


class Event < ActiveRecord::Base
  has_many :scales

  ...
end

what am I doing wrong?

+2  A: 

Fields_for is not a method for the enclosing form object, so change f.fields_for to simply fields_for.

Fred