如何使用无限子条目查看导航? [英] How to view navigation with infinit sub entries?
问题描述
我有一个名为Category的简单模型.该模型具有三个字段:name:string
,parent_id:integer
,并且原因为id
! parent_id
是自我参照关联:
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!
我不知道如何遍历每个类别children
和那些孩子的孩子,...
I dont get how I can loop through each categories children
and those childrens children,...
推荐答案
假设您拥有部分被调用的类别,则可以执行以下操作
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
我用过haml,但是在erb中会差不多.
I've used haml but it would be about the same in erb.
这篇关于如何使用无限子条目查看导航?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!