Rails:在表体中渲染部分

时间:2018-05-20 12:47:39

标签: 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

在我看来,我想为附加到报表的发票创建一个表,其中列名表示属性,如:

<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>

对于正文,我如何添加我的发票字段?(目前呈现的方式如下:)

<%= f.fields_for :invoices do |f| %>
      <%= render 'invoices_fields',  f: f %>

_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>

基本上我希望我的部分数据作为我表中的行(同时保留部分行)

1 个答案:

答案 0 :(得分:1)

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>

你的_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>

或者您甚至可以移动部分中的<tr>标签 - 可能会更有意义