【发布时间】: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