使用复选框过滤记录 [英] Using checkboxes to filter records
问题描述
我试图从零编程经验学习rails,并有一个应用程序,我试图做。在这里的人的帮助下,我有一个索引页的场地,可以通过下拉菜单通过它们属于哪个区域。我想能够有相同的影响,但使用复选框,还能够选择多个区域添加到过滤器。这是我到目前为止:
模型:
地点& ActiveRecord :: Base
belongs_to:user
has_many:reviews
belongs_to:area
scope:north,where(:area_id =>2)
scope:west,where(:area_id =>3)
scope:south,where(:region_id =>4)
end
控制器:
def index
pre>
if(params [:area]&&& Area.all.collect(&:name).include?(params [:area] [:name]))
@venues = Venue.send params [:area] [:name] .downcase)
else
@venues = Venue.all
end
end
Venues index.html.erb:
< div class =filter_options_container>
< form class =filter_form,method =get>
<%= select(area,name,Area.all.collect(&:name),{:prompt =>'All Areas'})%>< br&
< input type =submitvalue =Filter/>
< / form>
< br>< br>
< form class =filter_form,method =get>
<%Area.find(:all).each do | a | %>
<%= check_box_tag(area,name)%>
<%= a.name%>
<%end%>
< input type =submitvalue =Filter/>
< / form>
< / div>
< div class =venue_partials_container>
<%= render:partial => 'venue',:collection => @venues%>
< div class =clearall>< / div>
<%= link_to'添加新场地',new_venue_path,:class => add_new_venue_button%>
< / div>
filter_options_container中的第一个表单(下拉列表)工作正常,但复选框返回无法将Symbol转换为Integer我失踪/做错了什么?
谢谢任何帮助。
解决方案我不知道是什么导致你的错误,但我可以告诉你,
check_box_tag
帮助器不工作你期望它的方式。从文档,它的定义如下:check_box_tag(name,value =1,checked = false,options = {})
/ pre>
因此,重复调用
check_box_tag(area,name)
$ c>< input id =areaname =areatype =checkboxvalue =name/> 请注意,value属性(这是在提交表单时发送到您的服务器的值)始终为name - 而不是您想要的!
请尝试以下操作:
<%Area.all.each do | a | %>
<%= check_box_tag(areas [],a.id)%>
<%= h a.name%>
<%end%>
我更改的东西:
- 我使用
Area.all
而不是Area.find(:all)
- I但是DanneManne的回答声称它已经在Rails 3中过时了(我不知道 - 我仍然在2.3.8)。
- 我使用区域的ID而不是其在值字段中的名称;如果可以 - ID是整数,并且比字符串比较快,并且在数据库中的id列上总是有一个索引用于超快速查找,那么通过ID查找事物总是好的。
- 最后,但最重要的是,在输入名称后面加入
[]
。这使得Rails收集使用这个名称提交的所有值到数组中,而不是只取最后一个。如下:
投掷网址...
/ whatever?a = 3& a = 17& a = 12
。 ..在一个Rails应用程序给你的params哈希...
{:a => 12}
...但是网址...
/ whatever?a [] = 3& a [] = 17& a [] = 12
...给你想要的:
{:a = >> [3,17,12]}
如果你有一个数组的所有area_ids有兴趣的人,你可以得到所有在任何在这些区域的一个线索的场地(不是Rails好吗?):
filtered_venues = Venue.all(:conditions => {:area_id => params [:areas]})
在调用finder之前,确保你有一个有效的区域ID数组 - 如果你不这样做,你的条件哈希将计算为{:area_id => nil} Rails会找到所有没有area_id的场所 - 可能没有,绝对不是你想要的。
希望这有助于!
I am trying to learn rails from zero programming experience and have an app im trying to make. With much help from people on here, I have an index page of venues which are filterable by which area they belong to via a dropdown menu. I would like to be able to have the same affect but using checkboxes and also being able to select multiple areas to add to the filter. This is what I have so far:
Model :
class Venue < ActiveRecord::Base belongs_to :user has_many :reviews belongs_to :area scope :north, where(:area_id => "2") scope :west, where(:area_id => "3") scope :south, where(:area_id => "4") end
Controller:
def index if (params[:area] && Area.all.collect(&:name).include?(params[:area][:name])) @venues = Venue.send(params[:area][:name].downcase) else @venues = Venue.all end end
Venues index.html.erb:
<div class="filter_options_container"> <form class="filter_form", method="get"> <%= select("area", "name", Area.all.collect(&:name), {:prompt => 'All Areas'}) %><br> <input type="submit" value="Filter" /> </form> <br><br> <form class="filter_form", method="get"> <% Area.find(:all).each do |a| %> <%= check_box_tag("area", "name") %> <%= a.name %> <% end %> <input type="submit" value="Filter" /> </form> </div> <div class="venue_partials_container"> <%= render :partial => 'venue', :collection => @venues %> <div class="clearall"></div> <%= link_to 'add a new venue', new_venue_path, :class => "add_new_venue_button" %> </div>
The first form (the dropdowns) in the filter_options_container works fine but the checkboxes (the second form) is returning "can't convert Symbol into Integer" what am I missing/doing wrong?
Thankyou for any help.
解决方案I don't know exactly what's causing your error, but I can tell you that the
check_box_tag
helper isn't working the way you expect it to. From the documentation, it's defined like this:check_box_tag(name, value = "1", checked = false, options = {})
So calling
check_box_tag("area", "name")
repeatedly will just give you<input id="area" name="area" type="checkbox" value="name" />
multiple times. Note that the "value" attribute (which is the value that gets sent to your server when that form is submitted) is always "name" - not what you want!Instead, try the following:
<% Area.all.each do |a| %> <%= check_box_tag("areas[]", a.id) %> <%=h a.name %> <% end %>
The things I've changed:
- I used
Area.all
instead ofArea.find(:all)
- I did it for cosmetic reasons, but DanneManne's answer claims it's obsolete in Rails 3 (I wouldn't know - I'm still on 2.3.8).- I used the area's ID instead of its name in the value field; it's always good to look things up by ID if you can - IDs are integers, and compare faster than strings, and there'll always be an index on the id column in your database for extra-fast lookups.
- And last, but way most importantly I threw in
[]
after the input name. This lets Rails collect all the values submitted with this name into an array, rather than just taking the last one. See below:Throwing the URL...
/whatever?a=3&a=17&a=12
...at a Rails app gives you the params hash...
{:a => 12}
...but the URL...
/whatever?a[]=3&a[]=17&a[]=12
...gives you what you want:
{:a => [3, 17, 12]}
And if you have an array of all the area_ids you're interested in, you can get all the venues that are in any of those areas in a one-liner (isn't Rails great?):
filtered_venues = Venue.all(:conditions => {:area_id => params[:areas]})
Just make sure you have a valid array of area ids before calling that finder - if you don't, your conditions hash will evaluate to {:area_id => nil}, and Rails'll find you all the venues that don't have an area_id - probably none, and definitely not what you want.
Hope this helps!
这篇关于使用复选框过滤记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!