当使用Rails ActiveAdmin gem显示资源时,我想显示另一个关联模型的表.
所以让我们说一个酒庄has_many:产品.现在我想显示与Winery管理员资源的显示页面相关联的产品.而且我希望这是一个类似于产品资源索引的表格.
我得到它的工作,但只有通过手动重新创建HTML结构,哪种吮吸.为相关资源的特定子集创建索引表样式视图是否有更清洁的方法?
我有什么,有点吸吮:
- show title: :name do |winery|
- attributes_table do
- row :name
- row(:region) { |o| o.region.name }
- rows :primary_contact,:description
- end
- # This is the part that sucks.
- div class: 'panel' do
- h3 'Products'
- div class: 'attributes_table' do
- table do
- tr do
- th 'Name'
- th 'Vintage'
- th 'Varietal'
- end
- winery.products.each do |product|
- tr do
- td link_to product.name,admin_product_path(product)
- td product.vintage
- td product.varietal.name
- end
- end
- end
- end
- end
- end
解决方法
为了解决这个问题,我们使用了partials:
/app/admin/wineries.rb
- ActiveAdmin.register Winery do
- show title: :name do
- render "show",context: self
- end
- end
应用程序/管理/ products.rb
- ActiveAdmin.register Product do
- belongs_to :winery
- index do
- render "index",context: self
- end
- end
/app/views/admin/wineries/_show.builder
- context.instance_eval do
- attributes_table do
- row :name
- row :region
- row :primary_contact
- end
- render "admin/products/index",products: winery.products,context: self
- active_admin_comments
- end
/app/views/admin/products/_index.builder
- context.instance_eval do
- table_for(invoices,:sortable => true,:class => 'index_table') do
- column :name
- column :vintage
- column :varietal
- default_actions rescue nil # test for responds_to? does not work.
- end
- end