0

The models I am using are Merchant, Address, Phone, & Contact. The relationsships are as follows:

Merchant:

has_many :phones, :as => :phoneable, :dependent => :destroy
has_many :addresses, :as => :addressable, :dependent => :destroy
has_many :contacts, :as => :entity, :dependent => :destroy

Contact:

has_many :phones, :as => :phoneable, :dependent => :destroy
has_many :addresses, :as => :addressable, :dependent => :destroy

I have a form where one can create a parent record with two address records and many phone records. My merchants controller new method has this setup code:

@merchant.addresses.build (:address_type => "Dba")
@merchant.addresses.build (:address_type => "Mailing")
@merchant.phones.build (:phone_type => "Business")
@merchant.phones.build (:phone_type => "Fax")

The actual form has;

<%= f.fields_for :addresses do |a| %>

for the two addresses for the merchant, and;

<%= f.fields_for :phones do |p| %>

for the merchant's phone records.

The problem I am having is when adding the contacts it's address and phone records;

<%= f.fields_for :contacts do |c| %>
<%= f.fields_for :addresses do |a| %>
<%= f.fields_for :phones do |p| %>

which renders two addresses and phones for each contact record.

I only want the two addresses for the merchant record. How do I get the two addresses.build & phones.build to create records only for the parent and build only one address & phone record for each contact (which will be added to the form dynamically via a link)?

4

1 回答 1

0

一种方法是让商家的“新”操作显示一个表单,该表单显示两个要由用户填写的地址。此表单的提交操作将创建商家。它还将有一个“添加联系人”的链接,该链接将显示另一种创建联系人的表单。联系人的“新建”表格应显示用户填写的一个地址和一个电话号码。

于 2012-04-27T04:49:14.330 回答