Reputation: 4028
I have a list of movies as well their ratings. At the top of my page I have a form that gives a list of checkboxes that shows each available rating (G, PG-13, etc). Once a user clicks a checkbox(es) and hits submit I just want the selected movies to show up.
In my index method I have an instance variable called @filtered_ratings
that collects the keys from the checked movies. My idea was to alter @movies
in my controller using the find method and matching the keys from @filtered_ratings
to the list of movies. However, I think I've been doing this incorrectly as I can't get it to work. I've tried several ways such as @movies=Movie.find(params[:id] = @filtered_ratings)
but I know this is incorrect. Any suggestions?
Upvotes: 3
Views: 5543
Reputation: 8892
Assuming your checkbox form is something like:
<%= form_tag method: (blah), url: (blah) do %>
<%= check_box_tag 'ratings[]', "R" %>
<%= check_box_tag 'ratings[]', "PG" %>
<%= check_box_tag 'ratings[]', "PG-13" %>
<%= submit_tag 'Submit' %>
<% end %>
you would get params[:ratings] = ["R", "PG"] if say the first two boxes were checked. So, in the controller you could just do
Movie.where("rating IN (?)", params[:ratings])
. This assumes your movies have a 'rating' attribute.
Upvotes: 7