How to automatically generate Ruby on Rails navigation menu

Source: Internet
Author: User
Tags ruby on rails

The following issues have been encountered recently in a ROR Web project:

Products can be classified by class navigation browsing, can be divided into three types of a,b,c, three categories under the other categories, at the same time, the base class and its classification can be extended by the user. Horizontally, the main class can be extended by the user, and the user can extend it vertically.

Category information Store and Categories table:

Id:integer PRIMARY Key

Name:string category Name

The parent class of the Parentid:integer category

Requirements:

A menu that generates a navigation menu and can contain subclasses can be clicked to expand or close, and the product can be navigated by category.

To implement the Ruby on Rails navigation menu:

Through depth precedence has historically generated menus, in the convenience of the process to build the menu HTML encoding, mainly using a @htmlmenu string to splice the generated HTML code, the final display in the page.

Ruby on Rails navigation menu code:

def index @htmlmenu = "" @htmlmenu + + "< ul>" @root = Category.find (: all,:conditions=>[' parentid=0 ')  
    ] @root. Each {|item| If Category.find_by_parentid (item.id) @htmlmenu + = "< li>< a href= ' #ChildMenu #{item.id} ' onclick=\ ' Domenu ( 
    ' Childmenu#{item.id} '/' > ' else @htmlmenu + + < li>< a href= '/categories/#{item.id} ' > ' End  
    @htmlmenu + + item.name @htmlmenu + = "</a>" Buildmenu item @htmlmenu + = "</li>"} @htmlmenu + + "</ul>" End private def buildmenu category @children = Category.find_all_by_parentid 
      (category.id) if @children. size!=0 @htmlmenu + + < ul id= ' childmenu#{category.id} ' class= ' collapsed ' > '  
        @children. Each {|item| 

If Category.find_all_by_parentid (item.id). size!=0 @htmlmenu + = "< li>< a href= ' #ChildMenu #{item.id} ' 
       Onclick=\ "Domenu (' childmenu#{item.id} ') \" > "Else         @htmlmenu + = "< li>< a href= '/categories/# {item.id} ' >" end @htmlmenu + = Item.name @htmlmenu + + "</a>" Buildmenu item @htmlmenu + = "</li>"} @html menu+= "</ul>" End

Related Article

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.