How to view navigation with infinit sub entries?

倖福魔咒の 提交于 2019-12-25 02:22:09

问题


Ive got a simple Model called Category. The model is has three fields : name:string, parent_id:integer and of cause an id! The parent_id is an self referential association:

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

So every Category can have subcategories and on and on and on,... This isnt a problem at the database but when it comes to the view Im getting confused!

I dont get how I can loop through each categories children and those childrens children,...


回答1:


Assuming you had a partial called category you could do something like this

%h2= category.name
%ul
  - category.children.each do |child|
    %li
      = render :partial => 'category', :object => child

I've used haml but it would be about the same in erb.



来源:https://stackoverflow.com/questions/8858659/how-to-view-navigation-with-infinit-sub-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!