你如何添加第二列儿童'没有重复父母的#39; Active Admin中的列条目?

时间:2016-02-06 02:45:17

标签: ruby-on-rails-4 activeadmin

此表Raw Table with correct output是我要查找的结果,但是如何将此代码块转换为Active Admin /app/admin/postal_code.rb索引视图正确输出表所需的任何代码。

目前此块以部分呈现,/ app / views / admin / sentcode / _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>

但/ app/admin/postal_code.rb中的此代码Active Admin Index view提供了多个父列条目,每个第二列子条目一个。

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

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

class Region < ActiveRecord::Base
validates :name, presence: true
has_many :postalcodes
# has_many :produces, :through => :produceregionmonths
end

1 个答案:

答案 0 :(得分:0)

以下是如何创建具有区域名称标题和内部相应邮政编码的单个表

# 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