联系模式
class Contact < ActiveRecord::Base
belongs_to :phonebook
has_many :mobiles
accepts_nested_attributes_for :mobiles
end
移动模型
class Mobile < ActiveRecord::Base
belongs_to :contact
end
联系人控制器
def new
@contact = Contact.new
end
注意:
如何使用rails helper编写表单以为新的联系人实例生成多个移动对象?
这是我的表格
<h1>Create a new Contact</h1>
<h2>Add details</h2>
<% form_for(@contact) do |f| %>
<%= f.error_messages %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :mobile %><br />
<% f.fields_for :mobile do |ff| %>
<div>
<%= ff.select :type,options_for_select([["HOME", "H"], ["WORK", "W"],["OTHER", "O"]])%>
<%= ff.text_field :details %>
</div>
<% end %>
</div>
<div class="actions">
<%= f.submit "Add Contact" %>
</div>
<% end %>