Grouping a list of links by category


#1

Hi,

I want to display a list of links grouped by category like this:
Group 1
Link 1
Link 2
Group 2
Link 1
Link 2

I have this in my controller:
def index
@links = Link.find(:all, :order => “category_id”)
end

And this in the view:

Can somebody give my some pointers how to achieve this?
Thanks!

Maarten


#2

Maarten,

On Fri, Apr 14, 2006 at 01:30:51PM +0200, Maarten H. wrote:

I want to display a list of links grouped by category like this:
Group 1
Link 1
Link 2
Group 2
Link 1
Link 2

What a coinsidence. I just set up a similar link section yesterday.

Can somebody give my some pointers how to achieve this? Thanks!

I’m very new to Rails so there it is very likely that there is a better
way
to do this. But, the way I did it was:

In my Link controller:

def list
    @categories = LinkCategory.find(:all, :order => 'name')
end

And in my list.rhtml view something along the lines of:

<% for category in @categories -%>

<h2><%= h(category.name) %></h2>

<% for link in category.links -%>

    <p><%= link_to h(link.name), link.url %></p>

<% end -%>

<% end -%>

Kevin
http://www.RawFedDogs.net
http://www.WacoAgilityGroup.org
Bruceville, TX