新手问题 使用 content_tag 输出 awesome_nested_set 树状结构

luffycn · 2014年12月10日 · 最后由 DavidWei 回复于 2014年12月11日 · 1644 次阅读

写在 helper 中的方法 怎么输出了?

抛个砖,先序遍历二叉树。中间的部分自己可以自己适当修改下。 另外你也可以参考:https://github.com/the-teacher/the_sortable_tree/blob/master/app/helpers/render_expandable_tree_helper.rb

def my_awesome_nested_tag
    comment_roots = Comment.roots 

    result = ""
    comment_roots.each do |comment|
      result << div_nested_tag(comment)
      end
    raw result
  end

  private
  def div_nested_tag(comment_root)
    result = ""
    if comment_root.present?
      result << "<div class='comment comment_#{comment_root.level}'> #{comment_root.content}"

      #取出所有的子节点
      dests =  comment_root.children
      dests.each do |dest|
        result << div_nested_tag(dest)
      end
      result << "</div>\n"
    end
    result
  end

之前有人也提到过,这个东西不适合做评论,对数据库的查询比较多。数据量大起来肯定就慢起来了。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号