I have two models
我有两个型号
class Report < ApplicationRecord
has_many :invoices
accepts_nested_attributes_for :invoices, allow_destroy: true
end
class Invoice < ApplicationRecord
belongs_to :report
end
In my view, I would like to create a table for the invoices attached to a report, where the columns name represent attributes, something like :
在我看来,我想为附加到报表的发票创建一个表,其中列名表示属性,如:
<table>
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Document</th>
<th>Description</th>
<th>Invoice sum</th>
<th>Gross Amount</th>
<th>Available budget</th>
</tr>
</thead>
For the body, how can I add my Invoices fields?( which at the moment are rendered like :)
对于正文,我如何添加我的发票字段?(此刻呈现为:)
<%= f.fields_for :invoices do |f| %>
<%= render 'invoices_fields', f: f %>
Where _invoices_fields.html.erb is
_invoices_fields.html.erb在哪里
<div class="inline input-field">
<%= f.text_field :invoice_date%>
</div>
<div class="inline input-field">
<%= f.text_field :invoice_type%>
</div>
<div class="inline input-field">
<%= f.text_field :description %>
</div>
<div class="inline input-field">
<%= f.text_field :invoice_category %>
</div>
<div class="inline input-field">
<%= f.text_field :invoice_sum %>
</div>
Basically I want that data from my partial as rows in my table(while keeping the partial )
基本上我希望我的部分数据作为我表中的行(同时保持部分)
1 个解决方案
#1
1
your_view.html.erb
your_view.html.erb
<table>
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Document</th>
<th>Description</th>
<th>Invoice sum</th>
<th>Gross Amount</th>
<th>Available budget</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :invoices do |f| %>
<tr>
<%= render 'invoices_fields', f: f %>
</tr>
</tbody>
</table>
Your _invoices_fields.html.erb partial
你的_invoices_fields.html.erb部分
<td class="inline input-field">
<%= f.text_field :invoice_date%>
</td>
<td class="inline input-field">
<%= f.text_field :invoice_type%>
</td>
<div class="inline input-field">
<%= f.text_field :description %>
</div>
<td class="inline input-field">
<%= f.text_field :invoice_category %>
</td>
<td class="inline input-field">
<%= f.text_field :invoice_sum %>
</td>
Or you can even move the <tr>
tags in the partial - would probably make more sense
或者你甚至可以在部分中移动标签 - 可能更有意义
#1
1
your_view.html.erb
your_view.html.erb
<table>
<thead>
<tr>
<th>#</th>
<th>Date</th>
<th>Document</th>
<th>Description</th>
<th>Invoice sum</th>
<th>Gross Amount</th>
<th>Available budget</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :invoices do |f| %>
<tr>
<%= render 'invoices_fields', f: f %>
</tr>
</tbody>
</table>
Your _invoices_fields.html.erb partial
你的_invoices_fields.html.erb部分
<td class="inline input-field">
<%= f.text_field :invoice_date%>
</td>
<td class="inline input-field">
<%= f.text_field :invoice_type%>
</td>
<div class="inline input-field">
<%= f.text_field :description %>
</div>
<td class="inline input-field">
<%= f.text_field :invoice_category %>
</td>
<td class="inline input-field">
<%= f.text_field :invoice_sum %>
</td>
Or you can even move the <tr>
tags in the partial - would probably make more sense
或者你甚至可以在部分中移动标签 - 可能更有意义