Recursive Spanning Tree

Source: Internet
Author: User
Code

Class Product < Activerecord: Base
Acts_as_tree: Order =>   " Name "

Def self. Roots
Self. Find (: All,: Conditions=> [ " Parent_id =? " , 0 ])
End

Def is_leaf ?  
Return   ! Self. has_children ?
End

Def is_root ?  
Return Self. parent_id =   0  
End
End

 

Code

Module productshelper
Def get_tree (trees)
Tree_html =   "" ;
Trees. Each Do   | Tree |  
# If Tree. is_root?
Tree_html + =   " <Li> <span> # {tree. name} & nbsp; # {link_to ('modify', edit_admin_product_path (tree. ID)} </span> "
# End

If   ! Tree. Children. Empty ? Then
Tree_html + =   " <Ul> "   + Get_tree (tree. Children) +   " </Ul> "
End
Tree_html + =   " </LI> "
End
Return Tree_html
End
End

 

 

View:

 

< Ul ID = " Browser "   Class = " Filetree " >
<% = Get_tree (@ products) %>
</ Ul >

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.