You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ActiveAdmin 3.0:在Store详情页渲染关联Employee的索引表格

在ActiveAdmin 3.0的Store详情页嵌入员工索引表格的简洁实现

可以利用ActiveAdmin内置的panel和table_for方法快速实现,无需自定义完整的表格逻辑、样式和HTML,具体步骤如下:

  1. 确保模型关联已正确配置:

    • Store模型:has_many :employees
    • Employee模型:belongs_to :store
  2. 在Store的ActiveAdmin配置中,修改show块,先保留默认的店铺属性展示,再添加员工表格面板:

ActiveAdmin.register Store do
  show do
    # 渲染店铺核心属性区域
    attributes_table do
      row :name
      row :address
      row :phone
      row :created_at
    end

    # 嵌入员工索引表格
    panel "店铺员工列表" do
      # 复用ActiveAdmin内置的表格样式与功能,直接关联员工集合
      table_for resource.employees, sortable: true, class: "index_table" do
        # 定义要显示的员工字段,可根据需求调整
        column :name
        column :position
        column :email
        column :hire_date
        # 添加默认的操作按钮(查看、编辑)
        actions
      end
    end
  end
end

额外说明

  • 上述代码会自动生成与ActiveAdmin索引页一致样式的表格,包含排序、操作按钮等功能,无需手动编写CSS或HTML结构。
  • 如果你的Employee资源已经在ActiveAdmin中定义了index块的列配置,也可以通过复用部分视图进一步简化:
    panel "店铺员工列表" do
      render partial: "admin/employees/index", locals: { collection: resource.employees }
    end
    

内容的提问来源于stack exchange,提问作者Andrew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 15:28:15