Rails ActiveAdmin:在同一视图中显示相关资源的表

Ale*_*yne 10 ruby ruby-on-rails activeadmin

show使用Rails的ActiveAdmin宝石荷兰国际集团的资源,我想告诉另一关联模型的表.

所以,让我们说一个Winery has_many :products.现在我想显示管理资源show页面上关联的产品Winery.而且我希望这是一个类似于我indexProducts资源的表格.

我让它工作,但只能通过手动重新创建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
Run Code Online (Sandbox Code Playgroud)

Jea*_*ano 17

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

/app/admin/wineries.rb

ActiveAdmin.register Winery do
  show title: :name do
    render "show", context: self
  end
end
Run Code Online (Sandbox Code Playgroud)

app/admin/products.rb

ActiveAdmin.register Product do
  belongs_to :winery
  index do
    render "index", context: self
  end
end
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

/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
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,我认为`table_for(collection)`是缺失的逻辑部分. (3认同)