Saturday, December 26, 2009

[Rails] Re: Creating a special UL LI menu and having issues with subpage

On Dec 25, 9:57 am, Alpha Blue <li...@ruby-forum.com> wrote:
> 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
>

First off, you'd be better off actually setting up some real
associations. Such as:

class Page < ActiveRecord::Base

belongs_to :parent, :class_name => 'Page'
has_many :sub_pages, :class_name => 'Page', :foreign_key =>
'parent_id', :order => 'position'

named_scope :main_pages, :conditions => 'pages.parent_id IS
NULL', :order => 'pages.position', :include => :sub_pages

end

The controller code basically disappears; you can stash the results of
Page.main_pages in an instance variable if you're hell-bent on
avoiding model calls in the view.

Here's a cleaned-up version of the view code:

http://gist.github.com/264005


>
> 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?

You didn't mention if the actual source from your original view
includes all the elements; I'd hazard that the CSS for the menus might
be dropping them all in a stack, which would produce the "only show
the last one" behavior you've described.

Hope this helps!

--Matt Jones

--

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate