rails无法在索引页中嵌套具有父窗体的嵌套窗体

时间:2016-08-03 11:38:24

标签: 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
              - customer.goods_address.each do |goods_address|
                %td= goods_address.delivery_address
                %td= goods_address.name
                %td= goods_address.address
                %td= goods_address.state



 问题是我得到了未定义的方法'每个'货物地址。我不知道如何解决这个问题。有人可以帮助我。谢谢你提前

1 个答案:

答案 0 :(得分:0)

可能是某些客户没有goods_address,因此无法在each对象上执行nil

尝试:

%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.goods_address.delivery_address
          %td= customer.goods_address.name
          %td= customer.goods_address.address
          %td= customer.goods_address.state