我有一个具有无限数量的类别和子类别的类别模型 (HABTM)。我之前一直在推出自己的解决方案,但现在我意识到我将不得不使用 Ancestry gem 来完成这项工作。当我完成自己的解决方案时,我能够构建一棵树,因为我在名为 top_level_category 的字段中有一个字段,因此我知道从哪里开始和 parent_id,因此我可以找到沿树向下的路。使用 Ancestry,只有一个字符串类型的列可供构建。是否已经有解决方案,以便我可以轻松地直观地选择一个类别并知道它在树中的位置?
我曾尝试使用 activeadmin-sortable-tree gem,并且可以让它在类别索引页面上工作,但在添加新类别时却不行。
sortable tree: true,
sorting_attribute: :name,
parent_method: :parent,
children_method: :children,
roots_method: :roots
form do |f|
f.inputs "Category", as: :sortable do
f.input :parent_id, :as => :select, :collection => ItemsCategory.all.map {|u| [u.name, u.id]}
f.input do
label :name # item content
actions
end
f.input :name
end
f.actions
f.semantic_errors
end