我创建了三个具有以下属性的表:
表名:um_org_data
,属性:id, org_name, org_description, webdomain
表名:addresses
,属性:id, offc_addr, um_org_datum_id
表名:phone_nos
,属性:offc_ph, offc_ext, address_id
现在我希望我的组织名称有多个地址意味着Organization 1:M addresses
地址有多个电话号码意味着address 1:M telephone_no
我的控制器中的代码如下:
um_org_datum.rb
class UmOrgDatum < ActiveRecord::Base
attr_accessible :org_description, :org_name, :webdomain, :addresses, :addresses_attributes
has_many :addresses
accepts_nested_attributes_for :addresses
end
地址.rb
class Address < ActiveRecord::Base
attr_accessible :offc_addr
belongs_to :um_org_datum
has_many :phone_nos
accepts_nested_attributes_for :phone_nos
end
phone_no.rb
class PhoneNo < ActiveRecord::Base
attr_accessible :offc_ph, :offc_ext
belongs_to :address
end
由于与组织和地址的关联工作正常......但在创建多个电话号码时遇到问题。
我的观点的代码如下:
<%= simple_nested_form_for(@um_org_datum) do |f| %>
<%= f.error_notification %>
<div class="container">
<div class="row">
<div class="span3 pull-right">
<div class="well">
<h2>Heading</h2>
<p>Sample text</p>
</div>
</div>
<div class="span9">
<%= simple_nested_form_for(@um_org_datum, :validate => true, :html => { :class => 'form-horizontal' }) do |f| %>
<div class="control-group">
<label class="control-label">Organization Name </label>
<div class="controls">
<div class="input-prepend">
<%= f.text_field :org_name, required: true, :autofocus => true %>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Orgazination Description </label>
<div class="controls">
<%= f.text_area :org_description, :cols => "100", :rows => "10" %>
</div>
</div>
<div class="control-group">
<label class="control-label">Web Domain </label>
<div class="controls">
<div class="input-prepend">
<%= f.text_field :webdomain, required: true, :autofocus => true %>
</div>
</div>
</div>
<div class="control-group">
<label class="control-label">Office Address</label>
<div class="controls">
<%= f.link_to_add "<i class='icon-plus'></i>".html_safe, :addresses%>
<%= f.fields_for :addresses do |task_form| %>
<div class="input-prepend">
<%= task_form.text_field :offc_addr, :label => false, :placeholder => 'Office Address'%>
<div class="control-group">
<label class="control-label">Telephone</label>
<div class="controls">
<%= f.link_to_add "<i class='icon-plus'></i>".html_safe, :phone_nos%>
<%= f.fields_for :phone_nos do |task_form| %>
<div class="input-prepend">
<%= task_form.text_field :offc_ph, :label => false, :placeholder => 'Office Address'%>
</div>
<div class="input-prepend">
<%= task_form.text_field :offc_ext, :label => false, :placeholder => 'Office Extension'%>
</div>
<div class="input-prepend">
<%= task_form.link_to_remove "<i class='icon-remove'></i>".html_safe%>
<% end %>
</div>
</div>
<div class="input-prepend">
<%= task_form.link_to_remove "<i class='icon-remove'></i>".html_safe%>
<% end %>
</div>
</div>
<div class="control-group">
<label class="control-label">Office Phone Number </label>
<div class="controls">
<div class="input-prepend">
<%= f.text_field :offc_ph, required: true, :autofocus => true %>
</div>
</div>
</div>
<div class="control-group">
<div class="controls">
<% if @um_org_datum.id == nil%>
<button class="btn btn-primary" type="submit">Submit</button>
<% else %>
<button class="btn btn-primary" type="submit">Update</button>
<% end %>
<a class="btn" href="/um_org_data" style="text-color:black">Cancel</a>
</div>
</div>
<% end %>
</div>
</div>
</div>
<% end %>
现在我想要做的是我有三个相互依赖的循环organization
表。表具有在addresses
表中用作外键的主键,并且具有在表中用作外键的自己的主phone_nos
键。
我想要一个表单,当单击地址下的 + 按钮时,会显示带有 + 按钮的电话标签,用户可以在其中针对单个地址添加多个电话.. 就是这样。