我有三张桌子
1)客户端2)家庭3)员工
在以新的形式添加新字段的同时,一切都很完美
但在编辑表单中,如果我先为家庭添加嵌套属性,然后为员工添加嵌套属性,则为家庭添加的字段将消失,而为员工添加的字段将被添加
class Client < ActiveRecord::Base
self.primary_key = "id"
has_many :familys , dependent: :destroy, :foreign_key => 'client_id'
accepts_nested_attributes_for :familys , allow_destroy: true
has_many :employees , dependent: :destroy, :foreign_key => 'client_id'
accepts_nested_attributes_for :employees , allow_destroy: true
end
class Family < ActiveRecord::Base
belongs_to :client
end
class Employee < ActiveRecord::Base
belongs_to :client
end
#-------_form.html.erb-----------------------------#
<%= nested_form_for(@client) do |f| %>
<div><%= f.submit 'addfamily',:name => "add_n_tenpo" %></div>
<%= f.fields_for :familys do |w| %>
<tr>
<td class="label_width">巡店店舗</td>
<td><%= w.text_field :tenpo_code_1, class: 'form-control tenpoautofill' %></td>
<td><%= w.text_field :tenpo_code_2, class: 'form-control tenpoautofill' %></td>
</tr>
<% end %>
<div><%= f.submit 'addemployee',:name => "add_nw_tenpo" %></div>
<%= f.fields_for :employees do |nw| %>
<tr>
<td class="label_width">巡店店舗</td>
<td><%= nw.text_field :nw_tenpo_code_1, class: 'form-control' %></td>
<td><%= nw.text_field :nw_tenpo_code_2, class: 'form-control' %></td>
</tr>
<% end %>
<% end %>发布于 2017-10-16 18:42:53
因为您需要在嵌套形式中显式地传递familys和employees的值以用于编辑操作,所以这不会影响新操作的形式,因为在新操作中@client为空,因此它需要传递用于编辑操作的值。
尝尝这个
<%= nested_form_for(@client) do |f| %>
<%= f.fields_for :familys_attributes, @client.familys do |w| %>
<tr>
<td class="label_width">巡店店舗</td>
<td><%= w.text_field :tenpo_code_1, class: 'form-control tenpoautofill' %></td>
<td><%= w.text_field :tenpo_code_2, class: 'form-control tenpoautofill' %></td>
</tr>
<% end %>
<%= f.fields_for :employees_attributes, @client.employees do |nw| %>
<tr>
<td class="label_width">巡店店舗</td>
<td><%= nw.text_field :nw_tenpo_code_1, class: 'form-control' %></td>
<td><%= nw.text_field :nw_tenpo_code_2, class: 'form-control' %></td>
</tr>
<% end %>
<% end %>https://stackoverflow.com/questions/46768079
复制相似问题