Emmek on Rails
2009-Jun-09 21:14 UTC
How to display categorized pages in the most effective way?
I am looking for the most effective way to display categorized pages. Page model: acts_as_tree belongs_to :theme (has attribute "title") Theme model: has_many :pages (has attribute "name") I''d like to display all the children (titles) of the current page but grouping these pages in themes they belong to. I imagine that code "extracts" unique themes from @page.children and then iterates through these themes and displays just pages (titles) that are children of the current page and belong to proper themes. This should print the following data: Theme_X - page_b - page_f - page_m Theme_Y - page_c - page_g - page_l Theme_ ... where page_? is the child of the current page. How to do it effectively? Any help appreciated. -- Posted via http://www.ruby-forum.com/.
Ar Chron
2009-Jun-10 12:30 UTC
Re: How to display categorized pages in the most effective way?
Emmek on Rails wrote:> > I imagine that code "extracts" unique themes from @page.children and > then iterates through these themes and displays just pages (titles) that > are children of the current page and belong to proper themes. >You might want to look at named_scopes... this sounds like a candidate for a something like page.theme_ordered_children, rather than just page.children. -- Posted via http://www.ruby-forum.com/.
Emmek on Rails
2009-Jun-11 16:05 UTC
Re: How to display categorized pages in the most effective way?
OK, I was given the complete solution. I have the following pieces of code: controller: @thematized_pages = @page.children.group_by(&:theme) view: <% @thematized_pages.each do |theme, pages| -%> <h3><%= theme ? theme.name : ''Others'' %></h3> <ul> <% pages.each do |page| %> <li><%= link_to h(page.title), h(page.parent.link_name+''/''+page.link_name) %></li> <% end -%> </ul> -- Posted via http://www.ruby-forum.com/.