我正在尝试为具有多个帐单地址和邮政地址的客户配置文件创建一个嵌套组。当我浏览到客户配置文件表单时,我看到了客户配置文件表单的字段,但我没有看到帐单地址表单的任何字段或邮寄地址表格。有任何想法吗?我的控制器、模型和视图如下。
目标是拥有一个可以有多个帐单地址和多个邮政地址的客户资料。
客户档案模型:
class CustomerProfile < ActiveRecord::Base
#These entries are required to create a nested model form (multiple models in one form)
validates_presence_of :customerNumber
validates_uniqueness_of :customerNumber
has_many :billing_addresses, :dependent => :destroy
has_many :postal_addresses, :dependent => :destroy
accepts_nested_attributes_for :billing_addresses, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true
accepts_nested_attributes_for :postal_addresses, :reject_if => lambda { |a| a[:content].blank?}, :allow_destroy => true
end
帐单地址型号:
class BillingAddress < ActiveRecord::Base
belongs_to :customer_profile
attr_protected :customerNumber
end
邮政地址模型:
class PostalAddress < ActiveRecord::Base
belongs_to :customer_profile
attr_protected :customerNumber
end
客户档案控制器:
def new
@customer_profile = CustomerProfile.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @customer_profile }
end
end
def create
@customer_profile = CustomerProfile.new(params[:customer_profile])
respond_to do |format|
if @customer_profile.save
format.html { redirect_to @customer_profile, notice: 'Customer profile was successfully created.' }
format.json { render json: @customer_profile, status: :created, location: @customer_profile }
else
format.html { render action: "new" }
format.json { render json: @customer_profile.errors, status: :unprocessable_entity }
end
end
end
客户资料表格:
<br />
<h3>Add new customer profile with Billing and Postal Address</h3>
<br />
<h4> Customer Profile </h4>
<%= form_for @customer_profile do |f| %>
<div class="field">
<%= f.label :customerNumber %><br />
<%= f.text_field :customerNumber %>
</div>
#.... a few other fields removed to keep this short
<br />
<h4> Billing Address </h4>
<%= f.fields_for :billing_addresses do |b| %>
<div class="field">
<%= b.label :addressLine1 %><br />
<%= b.text_field :addressLine1 %>
</div>
#.... a few other fields removed to keep this short
<% end %>
<br />
<h4> Postal Address </h4>
<br />
<%= f.fields_for :postal_addresses do |p| %>
<div class="field">
<%= p.label :addressLine1 %><br />
<%= p.text_field :addressLine1 %>
</div>
#.... a few other fields removed to keep this short
<br />
<% end %>
<%= f.submit %>
<% end %>