Ruby on rails Rails:在表体中渲染局部
我有两种型号Ruby on rails Rails:在表体中渲染局部,ruby-on-rails,ruby,forms,Ruby On Rails,Ruby,Forms,我有两种型号 class Report < ApplicationRecord has_many :invoices accepts_nested_attributes_for :invoices, allow_destroy: true end class Invoice < ApplicationRecord belongs_to :report end 类报告
class Report < ApplicationRecord
has_many :invoices
accepts_nested_attributes_for :invoices, allow_destroy: true
end
class Invoice < ApplicationRecord
belongs_to :report
end
类报告
在我看来,我想为附加到报告的发票创建一个表,其中列名称表示属性,类似于:
<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>
#
日期
文件
描述
发票金额
总额
可用预算
对于正文,如何添加发票字段?(目前呈现为:)
其中_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>
基本上,我希望部分数据作为表中的行(同时保留部分)your\u 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>
#
日期
文件
描述
发票金额
总额
可用预算
您的_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>
或者,您甚至可以移动部分中的
标记,这可能更有意义
<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>