问题
This table, Raw Table with correct output, is the result I am looking for, but how do I translate this block of code into whatever Active Admin /app/admin/postal_code.rb index view requires to output the table correctly.
Currently this block is rendered in a partial, /app/views/admin/postalcode/_showtable.html.erb
<h1>Postal Code per Region</h1>
<table>
<tr>
<th>Region</th>
<th>Postal Code</th>
</tr>
<% @region.each do |region| %>
<tr>
<td><%= region.name %></td>
<td>
<table>
<% list = @postalcode.where("region_id=#{region.id}") %>
<% list.each do |l| %>
<tr>
<td width="5%">
<%= l.postalcode %>
</td>
</tr>
<% end %>
<% end %>
</td>
</table>
</table>
But this code, Active Admin Index view, in /app/admin/postal_code.rb is giving multiple parent column entries, one for each second column child entry.
ActiveAdmin.register PostalCode do
menu parent: "Region Settings"
permit_params :postalcode, :region_id
index do
column :id
column :region
column "Postal Code" do |region|
region.postalcode
end
end
In app/models/postal_code.rb
class PostalCode < ActiveRecord::Base
belongs_to :region
validates :region_id, presence: true
validates :postalcode, presence: true
# scope :region, -> (name) { }
# PostalCode.all.group_by(&:region_id)
end
and in app/models/region.rb
class Region < ActiveRecord::Base
validates :name, presence: true
has_many :postalcodes
# has_many :produces, :through => :produceregionmonths
end
回答1:
Here's how you can create those individual tables with header of the region name and the corresponding postalcodes inside
# app/admin/postal_code.rb
index do
Region.all.each do |region|
table_for(PostalCode.where(region_id: region.id), class: 'index_table') do
column region.name do |postal_code|
postal_code.postalcode
end
actions
end
end
end
来源:https://stackoverflow.com/questions/35236752/how-do-you-add-a-second-column-of-children-without-duplicating-the-parent-co