macfizz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Jan-12 21:18 UTC
Filtering the data displayed in a list
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 %> <tr valign="top" class="<%= cycle(''list-line-odd'', ''list-line-even'') %> "> <td> <span class="list-title"><%=h(centre.name) %> [<%h(centre.school.code) %>]</span><br /> <span class="list-actions"> <%= 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 %> </span> </td> <td> <span class="list-name"><%= h(centre.fname) %> <%h(centre.lname)%></span> </td> </tr> <% end %> --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---