Rails多个选择框问题用于搜索

时间:2010-04-01 19:35:09

标签: ruby-on-rails search multiple-select

首先,我的模型,控制器,视图:

我的模型,这是我的搜索代码:---------------------------

def self.find_by_lcc(params)
where = []
where << "category = 'Land'"
unless params[:mls].blank?
where << "mls = :mls" 
end
unless params[:county].blank?
where << "county = :county"
end 
unless params[:acreage_range].blank?
where << "acreage_range = :acreage_range" 
end
unless params[:landtype].blank?
where << "landtype = :landtype" 
end
unless params[:price_range].blank?
where << "price_range = :price_range" 
end

if where.empty?
[]
else
find(:all,
:conditions => [where.join(" AND "), params],
:order => "county, price desc")
end
end

我的控制器:----------------

def land
 @counties = ['Adams', 'Alcorn', 'Amite', 'Attala']         
 @title = "Browse"
 return if params[:commit].nil?
 @properties = Property.find_by_lcc(params)
 else
 'No properties were found'

render:action =&gt; 'land_table'       端

我的观点:----------------------

<table width="900">
 <tr>
<td>
<% form_tag({ :action => "land" }, :method => "get") do %>
<fieldset>
<legend>Search our Land Properties</legend>
<div class="form_row"><p>&nbsp;</p></div>
<div class="form_row">
<label for="mls">MLS Number:</label>&nbsp; 
<%= text_field_tag 'mls', params[:mls] %>
</div>
<div class="form_row">
<label for "county"><font color="#ff0000">*County:</font></label>&nbsp; 
<%= select_tag "county", options_for_select(@counties), :multiple => true, :size => 6  %>
</div>

 <div class="form_row">
<label for "acreage_range">Acreage:</label>&nbsp; 
 <%= select_tag  "acreage_range", options_for_select([['All',''],['1-10','1-10'],['11-25','11-25'],['26-50','26-50'],['51-100','51-100']]) %>
</div>

<div class="form_row">
<label for "landtype">Type:</label>&nbsp; 
 <%= select_tag  "landtype", options_for_select([['All',''],['Waterfront','Waterfront'],['Wooded','Wooded'],['Pasture','Pasture'],['Woods/Pasture','Woods/Pasture'],['Lot','Lot']]) %>
</div>

<div class="form_row">
<label for="price_range"><font color="#ff0000">*Price:</font></label>&nbsp; 
<%= select_tag "price_range", options_for_select([['All',''],['0-1,000','0-1,000'],['1,001-10,000','1,001-10,000'],['10,001-50,000','10,001-50,000'],['50,001-100,000','50,001-100,000'],['100,001-150,000']])%>
 </div>
<input type="text" style="display: none;" disabled="disabled" size="1" />
<%= submit_tag "Search", :class => "submit" %>
</fieldset>

<% end%>
</td>

</tr>
</table>

搜索工作正常,直到我添加“,:multiple =&gt; true,:size =&gt; 6”,以使县字段多次选择。然后我收到错误:

Processing PublicController#land (for 65.0.81.83 at 2010-04-01 13:11:30) [GET]
  Parameters: {"acreage_range"=>"", "commit"=>"Search", "county"=>["Adams", "Amite"],      "landtype"=>"", "price_range"=>"", "mls"=>""}

ActiveRecord::StatementInvalid (Mysql::Error: Operand should contain 1 column(s):          SELECT * FROM `properties` WHERE (category = 'Land' AND county =     'Adams','Amite')  ORDER BY county, price desc):
  app/models/property.rb:93:in `find_by_lcc'
  app/controllers/public_controller.rb:84:in `land'
  /usr/lib/ruby/1.8/thread.rb:135:in `synchronize'
  fcgi (0.8.7) lib/fcgi.rb:117:in `session'
  fcgi (0.8.7) lib/fcgi.rb:104:in `each_request'
  fcgi (0.8.7) lib/fcgi.rb:36:in `each'
  dispatch.fcgi:24

我试图将county,acreage_range和price_range字段分成多个选择框,但无法使任何方法正常工作。任何帮助将不胜感激。

谢谢,

2 个答案:

答案 0 :(得分:0)

尝试

unless params[:county].blank?
   where << "county IN (:county)"
end

编辑#1

我相信即使选择了“ALL”选项,它也能正常工作

unless params[:county].blank? || params[:county] == "ALL"
   where << "county IN (:county)"
end

编辑#2

我认为all选项位于county。试试这个:

unless params[:county].blank?
  where << "county IN (:county)"
end

unless params[:acreage_range].blank? || params[:acreage_range] == "ALL"
   where << "acreage_range = :acreage_range" 
end

希望现在有效:]

答案 1 :(得分:0)

Try    

unless params[:county].blank?
  where << "county IN (:county)"
end

        OR

unless params[:county].blank?
  where << "county IN (  #{params[:county].join(',')})"
end