2017-10-16 56 views
0

加入第二嵌套形式的新字段,第一嵌套表單字段disappers我有三個表軌道而在編輯

1)客戶端 2)家族 3)emplyoee

同時加入在新形式寄託都新字段完美的作品

但在編輯形式,如果我添加嵌套屬性爲家庭第一 然後我增加對員工的嵌套屬性,那麼字段中添加家庭都不見了和字段添加員工

 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 %> 
+0

你可以張貼形式的代碼? –

+0

我已編輯的代碼 –

回答

0

,因爲你需要在嵌套形式編輯行動明確地傳遞值familysemployees,這不會影響你的新行動的形式,因爲在新的行動@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 %> 
+0

我得到這個錯誤 ::的ActionView ::模板錯誤(未定義的方法'tenpo_code_1' 爲#<::家庭ActiveRecord_Associations_CollectionProxy:0x007f6920a1ab68>): –

+0

@anjali你在''領域tenpo_code_1'家庭模式? – Gabbar

+0

你能說出家庭和員工模型中的字段嗎? – Gabbar