beginnersquestions
beginnersquestions

Reputation: 63

Rails: render partial in table body

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

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

Upvotes: 1

Views: 1123

Answers (1)

Kkulikovskis
Kkulikovskis

Reputation: 2088

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

<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

Upvotes: 1

Related Questions