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 %>
<%= link_to 'Show', :action => 'show', :id => centre %> | <%= link_to 'Edit', :action => 'edit', :id => centre %> | <%= link_to 'Delete', { :action => 'destroy', :id => centre }, :confirm => "Are you sure?", :method => :post %>