ruby-on-rails – Rails ActiveAdmin:在同一视图中显示相关资源的表

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails ActiveAdmin:在同一视图中显示相关资源的表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当使用Rails ActiveAdmin gem显示资源时,我想显示另一个关联模型的表.

所以让我们说一个酒庄has_many:产品.现在我想显示与Winery管理员资源的显示页面相关联的产品.而且我希望这是一个类似于产品资源索引的表格.

我得到它的工作,但只有通过手动重新创建HTML结构,哪种吮吸.为相关资源的特定子集创建索引表样式视图是否有更清洁的方法

我有什么,有点吸吮:

  1. show title: :name do |winery|
  2. attributes_table do
  3. row :name
  4. row(:region) { |o| o.region.name }
  5. rows :primary_contact,:description
  6. end
  7.  
  8. # This is the part that sucks.
  9. div class: 'panel' do
  10. h3 'Products'
  11. div class: 'attributes_table' do
  12. table do
  13. tr do
  14. th 'Name'
  15. th 'Vintage'
  16. th 'Varietal'
  17. end
  18. winery.products.each do |product|
  19. tr do
  20. td link_to product.name,admin_product_path(product)
  21. td product.vintage
  22. td product.varietal.name
  23. end
  24. end
  25. end
  26. end
  27. end
  28. end

解决方法

为了解决这个问题,我们使用了partials:

/app/admin/wineries.rb

  1. ActiveAdmin.register Winery do
  2. show title: :name do
  3. render "show",context: self
  4. end
  5. end

应用程序/管理/ products.rb

  1. ActiveAdmin.register Product do
  2. belongs_to :winery
  3. index do
  4. render "index",context: self
  5. end
  6. end

/app/views/admin/wineries/_show.builder

  1. context.instance_eval do
  2. attributes_table do
  3. row :name
  4. row :region
  5. row :primary_contact
  6. end
  7. render "admin/products/index",products: winery.products,context: self
  8. active_admin_comments
  9. end

/app/views/admin/products/_index.builder

  1. context.instance_eval do
  2. table_for(invoices,:sortable => true,:class => 'index_table') do
  3. column :name
  4. column :vintage
  5. column :varietal
  6. default_actions rescue nil # test for responds_to? does not work.
  7. end
  8. end

猜你在找的Ruby相关文章