如何让Active Admin在模型中显示代码DRY?

时间:2014-07-21 23:00:31

标签: activeadmin dry

我在Active Admin中有许多模型,这些模型的显示页面非常相似(但不完全相同):

  show do |ad|
    attributes_table do
      row :name
      row :length
      row :width
      row :height
      ...
    end

    panel "Images" do
      text_node link_to 'Add Image', new_admin_image_path(...)
      table_for ad.images do
        column "Image" do |image|
          image_tag(...)
        end
        column do |data|
          link_to :edit, edit_admin_image_path(...)
        end
        column do |data|
          link_to :delete, admin_image_path(data), method: :delete
        end
      end
    end
  end

'面板"图像"做'代码将在每个模型中完全复制,因此我想把它放在其他地方。我一直在沿着ViewHelper走下去并渲染部分路径,但在这两种情况下我最终得到的东西都不知道" panel"," text_node", " table_for"等等。关于做这件事的正确方法的指导?

1 个答案:

答案 0 :(得分:1)

ActiveAdmin使用的模板语言

Arbre支持部分。您可以 将重复的代码移动到arb部分,例如 app/views/admin/_images_panel.html.arb。然后是您的ActiveAdmin资源 只需使用部分路径和任何所需的本地路径调用render即可 变量

show do
  attributes_table do
    # ...
  end

  render 'admin/images_panel', data: data
end

部分也可以引用通用方法resource来消除 需要传入局部变量。资源是无论什么资源 管理员正在管理。例如:

panel "Images" do
  table_for resource.images do
    # Note use of `resource` instead of `ad` above.
    # ...
  end
end