在Active Admin中显示closure_tree。如何创建分层视图?

时间:2013-08-07 10:34:01

标签: ruby-on-rails activerecord activeadmin acts-as-tree

最小化我想在索引表中实现一些缩进。像这样:

+Parent
+--Child
   +--Child of Child
+--Child

所以我创建了以下内容:

ActiveAdmin.register Section do
  config.filters = false

  index do
    column :name do |s|
      " #{ "――" * s.depth } #{s.name}"
    end
    default_actions
  end

  controller do
    def collection
      Section.some_method_to_get_things_in_right_order
    end
  end
end

需要some_method返回活动记录关系,但我没有成功。最终以this hacky方式结束。

1 个答案:

答案 0 :(得分:3)

sortable_tree activeadmin插件适用于闭包树。

https://github.com/zorab47/active_admin-sortable_tree

它创建了一个简单的分层和可排序视图。

只需将以下内容放入树模型资源(app / admin / TreeModel): (假设Rails> 4,将< ..>替换为您的值)

ActiveAdmin.register TreeModel do

config.filters = false # the default filters don't work unfortunately
permit_params <YOUR_TREE_MODEL_ATTRIBUTES>

sortable tree: true,
               sorting_attribute: :<YOUR_SORT_ATTRIBUTE>,
               parent_method: :parent,
               children_method: :children,
               roots_method: :roots

index :as => :sortable do
    label :name # item content
        actions
  end

end