我基于 ORM 自关联实现了一个无限级分类,貌似都 ok,但最后在显示的时候遇到一点小问题,求解。 一。先上 model 字段有三个,id name parent_id
class Cat < ActiveRecord::Base
    belongs_to :parent, :class_name => "Cat", :foreign_key => "parent_id"
    has_many :childs, :class_name => "Cat", :foreign_key => "parent_id"
end
二.controller index,目的是显示所有分类
def index
  @cats = Cat.where :parent_id => 0
end
三.view index
<ul>
  <% @cats.each do |cat| %>
    <li>
      <%= cat.name %><%= CatsHelper.get_children(cat) %>
    </li>
  <% end %>
</ul>
四.helper 目的是获取子分类,并 html 化,调用是 view 里面的<%= CatsHelper.get_children(cat) %>
module CatsHelper
  def self.get_children(cat)
    if cat.childs != []
      cat_tree = "<ul>"
      cat.childs.each do |cat|
        cat_tree += "<li>#{cat.name}"
        CatsHelper.get_children(cat)
        cat_tree += "</li>"
      end
      cat_tree += "</ul>"
      return cat_tree
    end
  end
end
五。显示结果
 他的 html 标签还在,没有解析,这是为什么?怎么解决呢?谢谢
他的 html 标签还在,没有解析,这是为什么?怎么解决呢?谢谢