ActiveAdmin 3.0:在Store详情页渲染关联Employee的索引表格
在ActiveAdmin 3.0的Store详情页嵌入员工索引表格的简洁实现
可以利用ActiveAdmin内置的panel和table_for方法快速实现,无需自定义完整的表格逻辑、样式和HTML,具体步骤如下:
确保模型关联已正确配置:
- Store模型:
has_many :employees - Employee模型:
belongs_to :store
- Store模型:
在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
相关产品推荐
相关产品推荐

