在我的控制,我有:平鋪哈希中的訪問/輸出值?
@invoices = current_user.account.clients.map(&:invoices).flatten
,並在我的index.html.erb我想顯示的數值,目前我有以下幾點:
<% @invoices.each do |invoice| %>
<tr>
<td></td>
<td><%= invoice.client.name %></td>
<td><%= invoice.jobs.inspect %></td> #HERE IS WHERE I WANT START_DATE, etc
<td><%= invoice.created_at %></td>
<td><%= link_to "PDF", invoice_path(invoice, :format => :pdf)%></td>
<td><%= link_to invoice, :method => :delete, :confirm => 'Are you sure you want to delete this invoice' do %> <i class="icon-trash"></i><% end %>
</td>
</tr>
<% end %>
invoice.jobs.inspect返回以下內容:
[#<Job id: 15, start_date: "2012-03-24", due_date: "2012-03-31", notes: "grthryrth", audio_file_url: "1.txt", transcribed_file_url: "300.txt", status: "invoiced", account_id: 1, user_id: 1, client_id: 2, created_at: "2012-03-29 18:03:14", updated_at: "2012-03-29 18:40:49", invoice_id: 6>, #<Job id: 16, start_date: "2012-03-24", due_date: "2012-03-21", notes: "uopuiouioiio", audio_file_url: "Data Flow Analysis and Integration v1.1.docx", transcribed_file_url: "ECA-Sponsor Monthly Agenda 20120105.doc", status: "invoiced", account_id: 1, user_id: 4, client_id: 2, created_at: "2012-03-29 18:05:24", updated_at: "2012-03-29 18:40:49", invoice_id: 6>]
如何單獨訪問每個值,例如,invoice.jobs.start_date?謝謝!
謝謝。因此,如果我在發票上有超過1份工作,我將如何展示該工作?通過使用invoice.jobs [0] .start_date顯然只返回第一個工作項目的值,而不是後面的項目。 – user464180 2012-03-29 19:54:44
在這種情況下,您將循環瀏覽集合,例如'invoice.jobs.each do | job | ...或[將它傳遞給部分](http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-collections)。後者更可能是「Railsy」。 – 2012-03-29 20:31:00
謝謝,我很感激。 – user464180 2012-03-29 20:44:52