【问题标题】:Undefined method `customer_name' in nested association in RailsRails中嵌套关联中未定义的方法“customer_name”
【发布时间】:2018-12-04 16:45:52
【问题描述】:

我是 Rails 的新手,需要您的帮助。 我有工作模型、发票模型和项目模型。

job.rb 模型有 - has_one :invoice, dependent: :destroy accepts_nested_attributes_for :invoice

invoice.rb 有 - belongs_to :job, optional: true, :foreign_key => 'job_id' has_many :items, dependent: :destroy accepts_nested_attributes_for :items

item.rb 有 - belongs_to :invoice, optional: true, :foreign_key => 'invoice_id'

一切正常,它创建工作和发票,其中包含项目和发票显示它生成发票,但在发票/索引中我想在表格中列出所有发票但我无法得到 = @invoice.job.customer_name.capitalize 它给了我一个错误 - NoMethodError in Invoices#index undefined method `customer_name' for nil:NilClass

这就像工作是零。 我的问题是为什么在 invoices/show.html.erb 中我可以访问
= @invoice.job.customer_name.capitalize 在索引中它给我错误?

正如您在 invoices/show.html.haml 中看到的,我有 = @invoice.job.customer_name.capitalize,它给了我正确的数据:

.container.invoice
  .row
    .col-sm-6
      - if  current_user.logo.present? 
        = image_tag current_user.logo.url(:thumb) 
      - else 
        = image_tag 'blank.jpg'
    .col-sm-3
    .col-sm-3
      %h3
        = current_user.full_name.titleize
      %p  
      = current_user.email
      %p
      = current_user.phone
  %hr
  .row
    .col-sm-6
      %h4
        Customer :
        = @invoice.job.customer_name.capitalize.html_safe
      %p
      %b
        Phone: 
      = @invoice.job.customer_tel
      %p
      %b
        Email:
      = @invoice.job.customer_email
    .col-sm-3
    .col-sm-3
      %p
      %b
        Invoice: #  
      = @invoice.id
      %p
      %b
        Date:
      = @invoice.created_at.strftime("%B %d, %Y")
  %hr
  .row
    .col-sm-12
      %table.table.table-striped.table-hover
        %thead
          %tr.sortable-table
            %th.col-sm-1
              Item Id
            %th.col-sm-3
              Item/Operation
            %th.col-sm-3
              Unit cost/Hr rate
            %th.col-sm-3
              Qty
            %th.col-sm-2
            %th.col-sm-3
              Amount
        %tbody
        
        - @invoice.items.each do |item|
          %tr.sortable-table
            %th
              = item.id
            %th
              = item.name 
            %th
              = number_to_currency item.unit_cost 
            %th
              = item.qty
            %th 
            %th
              - amount = number_to_currency(item.unit_cost * item.qty) 
              = amount
            
  %hr
  .row
    .col-sm-6
    .col-sm-3
    .col-sm-3
      %b
        Subtotal
        - sum = 0
        - @invoice.items.each do |item|
          - if item.qty?
            - subtotal = (item.unit_cost * item.qty)
            - sum += subtotal
        = number_to_currency(sum)  
      %p
      %b  
        Tax
        = number_to_currency(sum * (@invoice.tax/100))
      %p  
      %h4
        %b
          Total
          = number_to_currency((sum * (@invoice.tax/100)) + sum)
  %hr

= link_to 'Edit', edit_invoice_path(@invoice)
\|
= link_to 'Back', invoices_path

但在 invoices/_invoice.html.erb 我有错误:

%h1 Listing invoices

%table.table.table-striped.table-hover
  %thead
    %tr.sortable-table
      %th
        Id
      %th
        Job Id
      %th
        Item
      %th
        Unit cost
      %th
        Qty
      %th
        Amount
  %tbody
    = render @invoices
%br

= link_to 'New Invoice', new_invoice_path

<tr>
  <td>
    <%= invoice.id %>
  </td>
  <td>
    <%= invoice.job_id %>
  </td>
  <td>

  </td>
  <td>
    <%= invoice.unit_cost %>
  </td>
  <td>
    <%= invoice.qty %>
  </td>
  <td>
    <%= invoice.job.customer_name %>
  </td>
  <td>
    <%= link_to 'Details', invoice, class: 'btn btn-primary btn-xs' %>
  </td>
  <td>
    <%= link_to "", edit_invoice_path(invoice), id: "edit_#{invoice.id}", class: 'glyphicon glyphicon-pencil index-icons' %>
  </td>
  <td>
    
     <%= link_to "", invoice_path(invoice), method: :delete, id: "delete_invoice_#{invoice.id}_from_index", class: 'glyphicon glyphicon-trash index-icons', data: {confirm: 'Are you sure ?'} %>
  </td>
</tr>

谢谢,如果您需要更多代码,请告诉我。

更新:

[you see invoice Id 47 have job id  67][1]
[the job id 67 is exist][2]
[on the invoice show  I have name of customer][3]


  [1]: https://i.stack.imgur.com/ODAkb.png
  [2]: https://i.stack.imgur.com/uGK21.png
  [3]: https://i.stack.imgur.com/sRTWS.png

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    Invoices#index 中的 NoMethodError 未定义方法 `customer_name' for nil:NilClass。

    这表示您呼叫客户名称的位置:

    @invoice.job.customer_name.capitalize.html_safe
    

    你在 nil 上调用它,或者换句话说:

    @invoice.job
    

    返回零。如果发票没有与之关联的作业,则任何涉及发票作业的方法都会失败(除了 @invoice.job,它将返回 nil)。

    您的两个选项是确保每张发票在呈现之前都有与之关联的作业,或者将您的视图模板重写为:

    .container.invoice
      .row
        .col-sm-6
          - if  current_user.logo.present? 
            = image_tag current_user.logo.url(:thumb) 
          - else 
            = image_tag 'blank.jpg'
        .col-sm-3
        .col-sm-3
          %h3
            = current_user.full_name.titleize
          %p  
          = current_user.email
          %p
          = current_user.phone
      %hr
      .row
        .col-sm-6
          - if @invoice.job
            %h4
              Customer :
              = @invoice.job.customer_name.capitalize.html_safe
            %p
            %b
              Phone: 
            = @invoice.job.customer_tel
            %p
            %b
              Email:
            = @invoice.job.customer_email
          .col-sm-3
          .col-sm-3
            %p
            %b
              Invoice: #  
            = @invoice.id
            %p
            %b
    

    如果作业与@invoice 相关联,这只会显示视图的底部

    【讨论】:

    • 你也可以看看这篇文章吗? stackoverflow.com/questions/53604767/…
    • 感谢您的快速响应,我收到了“@invoice.job.customer_name”,它在 show.html.haml 上,我的问题在 index.html.haml,我没有获取“customer_name”,但它给了我“job_id”,这让我相信发票与工作相关
    • 不用担心。如果 invoices 中的每张发票都包含一个 job_id,那么这一定意味着您已经删除了发票的父作业,而没有删除发票。无论是什么原因,您的一张发票都没有与之关联的作业,您需要修复它以获取视图渲染
    • @kickButtowski +1 表示坚持我猜,会看看
    • 你是对的马克!!!!!!在没有工作之前,我保存了一些旧发票。我删除了所有旧发票,现在可以了。我的天,我太傻了。为此,我将头撞到墙上 3 次。非常感谢伙计。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    相关资源
    最近更新 更多