Filtering by Category
Displaying all unused photos might seem acceptable right now, but we have only nine photos. If there were 900, it would quickly become unusable. So, our final feature in this chapter will be to display only the unused photos in a particular category.
The first thing to do in our controller is to get a list of all
categories that can populate the drop-down selection box. Edit
app/controllers/slideshows_controller.rb, and add
this line to the end of the edit
method:
@all_categories = Category.find(:all, :order=>"name")
This line retrieves a list of categories that can populate a drop-down selection box that the user will need to display only those unused photos that are in the selected category.
Now, edit app/views/slideshows/edit.html.erb,
and add this right after the 'Play this
Slideshow' line:
<p>
<label for="category_id">Filter "Unused Photos" on this Category</label><br/>
<%= collection_select(:category, :id, @all_categories, :id, :long_name) %>
<%= observe_field(:category_id,
:frequency => 2.0,
:update => 'slideshow-photos',
:url => { :action => 'change_filter'},
:with => 'category_id' ) %>
</p>The collection_select
helper is normally used inside an HTML form, but here we are using
it because it conveniently knows how to display a collection in a
drop-down box. It will never be submitted as part of a form.
As shown, the observe_field helper checks the category drop-down box for changes every two seconds. When a change is detected, an Ajax request is fired off ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access