I’m having a little trouble with trying to get a UL menu working with
rails. It’s working somewhat, but not the way it needs to. I’ll
explain after I post the code.
First, I’ve created a page administration system similar to what was
created in the screencasts for learningrails:
http://www.buildingwebapps.com/learningrails
What I like about the page administration system is having the ability
to create pages on the fly without having to use a hard redirect to a
controller. This is especially good for pages which are not static and
always being changed. However, they use a standard menu system and do
not showcase a dropdown menu system. I wanted to create a dropdown menu
system instead.
The following code is in the application controller:
define get_pages_for_tabs
@tabs = Page.find_main
@subs = Page.find_sub
end
In the page model the code does the following:
this method defines the tab views for all tabs
def self.find_main
Page.find(:all, :conditions => [‘parent_id IS NULL’], :order =>
‘position’)
end
this method defines the subpage tab views for all tabs
def self.find_sub
Page.find(:all, :conditions => [‘parent_id IS NOT NULL’], :order =>
‘position’)
end
What it’s going to do simply put, is find all pages that do not have a
parent_id set to them, which infers that those pages are -parent- pages.
It sets those parent pages to @tabs.
It then finds all pages that do have parent_ids, which infers that those
pages are -subpages- of the parent. It sets those pages to @subs,
ordering them by position numbers set.
The following code below is listed in the application.html.erb file in
my view layouts.
-
<% @tabs.each do |page| -%>
<% if page.redirect? %>
- <%= link_to page.navlabel, {:action => page.action_name, :controller => page.controller_name, :name => page.name} -%> <% else %>
-
<%= link_to page.navlabel, view_page_path(page.name) %>
<% @subs.each do |subpage| -%>
<% if page.id == subpage.parent_id %>
- <%= link_to subpage.navlabel, view_page_path(subpage.name) %>
<% end %>
<% end -%>
So, looking at the code above, here’s what will happen:
If a page has a redirect box checked, it uses an MVC. If it doesn’t, it
uses a non-static database driven page instead.
The page model houses the following fields:
id, name, title, body, parent_id, navlabel, position, redirect,
action_name, controller_name
If a page is going to be a sub-nav from a parent page menu item, I’ll
simply define the parent_id for that subpage which will match the
parent.id.
The only problem with the code above is that it only displays one
sub-menu for each parent menu item. If I have a parent page id of say 4
and I create three subpages that have parent_id = 4, it should look at
the following code:
<% if page.id == subpage.parent_id %>
… and showcase it.
It does so for only one subpage though.
What am I doing wrong based on the code shown? If you have any further
questions about this, please let me know. Hopefully I provided all the
information needed.
By the way, Merry Christmas everyone.