在下拉列表中的树层次结构中显示 categories/sub-categories

Show categories/sub-categories in tree hierarchy inside a dropdown

我有一个类别 table,其中包含字段 id、name 和 parent_id。根类别有 parent_id 0。现在我想在下拉列表中显示类别列表,结构如下:

root_category
    first_sub_category
        sub_sub_category
        another_sub_sub_category
    second_sub_category
another_root_category
    first_sub_category
    second_sub_category

这是我的控制器:

def new
  @category = Category.new
end   

视图如下:

    <%= f.label :parent_category %>
    <% categories = Category.all.map{|x| [x.name] + [x.id]} %>
    <%= f.select(:parent_id, options_for_select(categories), {}, class: 'form-control') %>

请帮忙。

假设您可以获得给定类别的子项类似于:

has_many :children, :class_name => 'Category', :foreign_key => 'parent_id'

为类别创建一个方法以获取所有子级并按级别缩进每个子级:

def all_children2(level=0)
    children_array = []
    level +=1
    #must use "all" otherwise ActiveRecord returns a relationship, not the array itself
    self.children.all.each do |child|
      children_array << "&nbsp;" * level + category.name
      children_array << child.all_children2(level)
    end
    #must flatten otherwise we get an array of arrays. Note last action is returned by default
    children_array = children_array.flatten
end

那么在你看来:

<select>
    <option></option>
    <% root_categories.each do |category| %>
      <option><%=category.name%></option>
      <% category.all_children2.each do |child| %>
        <option><%=child.html_safe%></option>
      <% end %>
    <% end %>
</select>

我还没有 100% 测试过这个,但我确实建议它应该工作......

通过在 application_helper.rb 中添加这些函数解决了问题

def subcat_prefix(depth)
  ("&nbsp;" * 4 * depth).html_safe
 end

 def category_options_array(current_id = 0,categories=[], parent_id=0, depth=0)
  Category.where('parent_id = ? AND id != ?', parent_id, current_id ).order(:id).each do |category|
      categories << [subcat_prefix(depth) + category.name, category.id]
      category_options_array(current_id,categories, category.id, depth+1)
  end

  categories
end

并在我看来像这样使用它们

<%= f.select(:parent_id, options_for_select(category_options_array), {}, class: 'form-control') %>