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