Я довольно новичок в 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 в Invoices # index неопределенный метод `customer_name 'для 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
Но в счетах / _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