# Filtering the display in a list

**URL:** <https://www.ruby-forum.com/t/filtering-the-display-in-a-list/125843>\
**Category:** Rails\
**Created:** [January 12, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/filtering-the-display-in-a-list/125843 "2008-01-12T19:14:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![macfizz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@macfizz](https://www.ruby-forum.com/u/macfizz)\
**Post date:** [January 12, 2008, 7:14pm UTC](https://www.ruby-forum.com/t/filtering-the-display-in-a-list/125843/1 "2008-01-12T19:14:18Z")

</div>

Hi all,

I’d appreciate some guidance in the right way to filter the data  
displayed in a list. I have two models, school and centre (centre  
belongs\_to school and school has\_many centres). In the list action for  
centres at the moment it shows all of the centres for all of the  
schools. I would like to be able to add links on the page to allow the  
user to choose either all centres (as at present) or just the centres  
for a particular school. I have in mind links for “All centres | North  
| South | East | West”. I’d like to keep the result in the current  
page so if there is a way to have the default to retrieve all centres  
and the other links to retrieve the centres for the North, South, East  
and West schools respectively that would do nicely.

I have put the relevant pieces of code for the centres controller and  
the list view for centres (and happy to provide other code on  
request). I could probably hack this to get a result but I’d really  
like to understand the right way to do it.

Anyway, any help will be much appreciated.

Regards,

## macfizz.

In app/controllers/centres\_controller.rb:

def list  
@centre\_pages, @centres = paginate :centres, :per\_page =\>  
150, :order =\> :name  
# use all\_schools to get the list of schools  
# we can pick this up in views/centres/list.rhtml  
@all\_schools = School.find(:all, :order =\> :code)  
end

In app/views/centres/list.rhtml:  
\<% for centre in @centres %\>

\<%=h(centre.name) %\>&nbsp;[\<%= h(centre.school.code) %\>]  
 \<%= link\_to 'Show', :action =\> 'show', :id =\> centre %\>&nbsp;|&nbsp; \<%= link\_to 'Edit', :action =\> 'edit', :id =\> centre %\>&nbsp;| &nbsp; \<%= link\_to 'Delete', { :action =\> 'destroy', :id =\> centre }, :confirm =\> "Are you sure?", :method =\> :post %\> \<%= h(centre.fname) %\>&nbsp;\<%= h(centre.lname)%\> \<% end %\>
