首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails在添加第二个嵌套表单新字段时,第一个嵌套表单字段将在编辑中消失

rails在添加第二个嵌套表单新字段时,第一个嵌套表单字段将在编辑中消失
EN

Stack Overflow用户
提问于 2017-10-16 18:25:38
回答 1查看 29关注 0票数 0

我有三张桌子

1)客户端2)家庭3)员工

在以新的形式添加新字段的同时,一切都很完美

但在编辑表单中,如果我先为家庭添加嵌套属性,然后为员工添加嵌套属性,则为家庭添加的字段将消失,而为员工添加的字段将被添加

代码语言:javascript
复制
        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 %>
EN

回答 1

Stack Overflow用户

发布于 2017-10-16 18:42:53

因为您需要在嵌套形式中显式地传递familysemployees的值以用于编辑操作,所以这不会影响新操作的形式,因为在新操作中@client为空,因此它需要传递用于编辑操作的值。

尝尝这个

代码语言:javascript
复制
<%= 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46768079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档