Rails嵌套表单在索引页面中显示字段以及父表单

时间:2016-08-03 10:05:20

标签: ruby-on-rails ruby

index.html.haml

我有一个客户详细信息表单,其中有一个名为'货物地址的嵌套表格。我想在索引页面中显示所有客户详细信息。



 %tbody
        - @customer_details.each do |customer|
          %tr
            %td= customer.customer_name
            %td= customer.customer_id
            %td= customer.address
            %td= customer.state
            %td= customer.email




嵌套表格



= fields_for customer_detail do |f|
  = f.fields_for :goods_address do |t|
    .fieldset
      .row
        .col-sm-12
        = t.label :delivery_address,"Goods to be delivered at same address mention above?", class: "col-sm-6 control-label text-right"
        = t.check_box :delivery_address, autofocus: true, class: "toggle-two customer-goods col-sm-6", :value => "On"
    %br
    %div{id: 'customer_goods'}
      .main-center-pan.main-login-pan
        .fieldset
          .row
            .col-sm-12
            = t.label :name,"Name", class: "col-sm-3 control-label text-right"
            = t.text_field :name, autofocus: true, class: "col-sm-3"
            = t.label :contact_no,"Contact number", class: "col-sm-3 control-label text-right"
            = t.text_field :contact_no, autofocus: true, class: "col-sm-3"
        %br




父表单



= nested_form_for(@customer_detail) do |f|
      - all_views_side_error_messages!(@customer_detail)
      .form-alignment
        .fieldset
          .row
            .col-sm-12
            = f.label :customer_name,"Organisation/Customer Name", class: "col-sm-3 control-label text-right"
            = f.text_field :customer_name, autofocus: true, class: "col-sm-3"
            = f.label :residential_type,"Residential Type", class: "col-sm-3 control-label text-right"
            = f.check_box :residential_type, autofocus: true, "data-width" => "200", class: "toggle-two-resident select-resident col-sm-3"
        %br




问题是我不知道如何在索引中定义嵌套表单的字段。有人请帮助我。谢谢提前。

1 个答案:

答案 0 :(得分:0)

如果要显示child-attributes(goods_address) parent(customer_details),只需循环父对象的关联。

%tbody
- @customer_details.each do |customer|
  %tr
    %td= customer.customer_name
    %td= customer.customer_id
    %td= customer.address
    %td= customer.state
    %td= customer.email
    - if customer.goods_address.present?
        %td= customer.try(:goods_address).try(:delivery_address)
        %td= customer.try(:goods_address).try(:name)
        %td= customer.try(:goods_address).try(:contact_no)