簡體   English   中英

Rails ActiveAdmin:在同一視圖中顯示相關資源的表

[英]Rails ActiveAdmin: showing table of a related resource in the same view

當使用Rails ActiveAdmin gem show資源時,我想顯示另一個關聯模型的表。

讓我們說一個Winery has_many :products 現在,我想顯示Winery管理資源的show頁面上關聯的產品。 我希望它是一個類似於我在Products資源index上得到的表。

我得到它的工作,但只能通過手動重新創建HTML結構,哪種糟糕。 是否有更簡潔的方法為關聯資源的特定子集創建index表樣式視圖?

我有什么,有點糟糕:

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

app/admin/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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM