Filtering results based on multiple criteria

What’s the paradigm for providing a set of filters for search
results?

Here’s my example: I have a list of projects, and projects can be
filtered by manager, status (open, closed, in progress), start date,
etc. The filter settings are stored in the database using the
preferences plugin, which works very nicely. However, my code is
ugly, and I can’t detect when a filter has be unset, which makes me
think there is a better way.

ProjectsController:

the if statement always returns true even if show_closed_projects is

unchecked
def index
@projects = Projects.all
if defined? params[:show_closed_projects] &&
params[:show_closed_projects] == ‘on’
current_user.write_preference(:show_closed_projects, true)
@projects = @projects.closed # this is a named scope
else
current_user.wrirte_preference(:show_closed_projects, false)
end
end

views/projects/index.html.erb

this works – if the preference is set, the checkbox is checked,

otherwise, it’s not checked.

project filter options
> Show closed projects?

<%= submit_tag "save filters" %> <%= link_to "cancel" %>

…projects listed here…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs