开发者

How do I filter data with check boxes? (rails)

开发者 https://www.devze.com 2023-04-06 03:32 出处:网络
I\'m creating a searchable version of my university\'s course listings, and am not sure how to use check boxes to let users filter classes by the day of the week. For instance, if users want to see ju

I'm creating a searchable version of my university's course listings, and am not sure how to use check boxes to let users filter classes by the day of the week. For instance, if users want to see just those classes that meet on Monday and Wednesday, they would check off the "M" box and the "W" box.

I basically already have everything working, but with a pull-down menu (in which they can select one day of the week) and not with check boxes.

courses http://img90.imageshack.us/img90/8626/courses.jpg

Here's how it works:

Each time the user clicks the "search" button, I create a new search object (which has attributes like class_name, instructor, day, etc.), and show only those courses whose attributes match the form submission. That is, if the user selects "M", I show those courses with "MWF", "MW", etc. Here's my view code:

<td><%= f.select :day, [['Any', nil],['M', 'M'], ['T', 'T'], ['W', 'W'], ['Th', 'Th'], ['F', 'F']]  %></td>

And here's part of my search model code:

def courses
    @courses ||= find_courses
end

def find_courses
  Course.find(:all, :conditions => conditions)
end

def day_conditions
  ["courses.day LIKE ?", "%#{day}%"] unless day.blank?
end

How can I use check boxes, instead of the pull-down menu, to filter by day? I want to let users check off "M" and "W", for example, which will show classes with "MW" and "MWF", but not "MT."

Currently my :day attribute is a string. Do I need to change it to an array? If so, how can I add an array into my sqlite table? "Array" doesn't seem to be a datatype.

I know I'm asking a more open-ended question here. Please let me know if more info would be helpful. Thanks!


EDIT: Followed Klochner's advice, but still not sure how to set up my form to have check-boxes form an array. Here's my (bad) code so far:

<td><%= fields_for @search do |form| %>
            M: <%= form.check_box :day, {}, 'M', ''  %>
            T: <%= form.check_box :day, {}, 'T', '' %>
            W: <%= form.check_box :day, {}, 'W', '' %>
            Th: <%= form.check_box:day, {}, 'Th', '' %>
            F: <%= form.check_box :day, {}, 'F', '' %>
    <% end %>

I'd like it so that if the user clicks M and W, days = [M, W] is passed to my search model. Right now this code is clearly wrong, as it just picks up the values from the last row (F:...). I've been googling a ton for the answer, but still can't fin开发者_开发知识库d anything. Should I be using fields_for here?


days = ["m","w","f"]
Course.where(["courses.day like ?","%"+days.join("%")+"%"])

To get the days, try <%= form.check_box "days[]", {}, "M", '', %>

For an explanation see here: http://www.skuunk.com/2008/05/checkbox-arrays-in-rails.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号