Checkboxes for filtered search


#1

I have a page that renders a series of about 20 checkboxes on the
left hand side with content on the right. These checkboxes are
created from a DB and that part is all working well.

What I want to do is have a user click on a checkbox [genres] and
have the content on the right be filtered based on the checkbox
ticked. If a user clicks on more than one checkbox than only those
stories are shown.

So for example the tables are like this :

Stories
id
title
genre_id


Genre
id
description


genre_stories
stories_id
genre_id

How do I achieve this? I also want the checkboxes checked after the
page is refreshed to show the user the current filter applied.

Thanks,

Rob


#2

Robert Z. wrote:

I have a page that renders a series of about 20 checkboxes on the
left hand side with content on the right. These checkboxes are
created from a DB and that part is all working well.

What I want to do is have a user click on a checkbox [genres] and
have the content on the right be filtered based on the checkbox
ticked. If a user clicks on more than one checkbox than only those
stories are shown.

How do I achieve this? I also want the checkboxes checked after the
page is refreshed to show the user the current filter applied.

Thanks,

Rob

You’re gonna want “form_remote_tag”, the idea is that you have an
onclick event for the checkbox that uses javascript to submit the form
data via AJAX. The AJAX request then updates the right hand side of the
page.

As for keeping the checkboxes checked, from the action that handles the
AJAX request, set the session with something like “session[:search] =
params[:checkboxes]”