2016-08-03 51 views
1

index.html.hamlRails的嵌套在索引頁的形式顯示的字段與父窗體

我有一個客戶的細節形成有一個名爲「商品地址」的嵌套形式一起。我想在索引頁面中顯示所有客戶詳細信息。

%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

問題是我不知道如何定義嵌套表單的字段在index.can有人plz幫助我。提前感謝。

+0

如果我理解正確,你想在父控制器的索引操作上顯示孩子的屬性,對吧? –

+0

yes.u是對的 –

回答

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

它顯示未定義的方法每個爲nilclass錯誤 –

+0

仍然我得到相同的錯誤。但是這次不適用於nilClass。未定義的方法'每個'爲#得到像這樣的錯誤。 –

+0

凱,你有'customer => has_one:goods_address'關係? – Sravan