如何在不复制Active Admin中的“父母”列条目的情况下添加“孩子”的第二列? [英] How do you add a second column of 'children' without duplicating the 'parent' column entry in Active Admin?
问题描述
此表具有正确输出的原始表,是我要查找的结果,但是如何将这段代码转换为Active Admin /app/admin/postal_code.rb索引视图所需的内容,才能正确输出该表。
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.
当前,该块是在部分/app/views/admin/postalcode/_showtable.html.erb
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>
但是此代码 Active Admin Index视图提供了多个父列条目,每第二个子列条目一个。
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
在app / models / postal_code.rb
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
并在app / models / region.rb
and in app/models/region.rb
class Region < ActiveRecord::Base
validates :name, presence: true
has_many :postalcodes
# has_many :produces, :through => :produceregionmonths
end
推荐答案
在这里您可以使用区域名称的标题和相应的邮政编码创建这些单独的表
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
这篇关于如何在不复制Active Admin中的“父母”列条目的情况下添加“孩子”的第二列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!