首先這裏是我的模型,控制器,視圖:Rails多個選擇框問題搜索
我的模型,這是我的搜索代碼:------------- --------------
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'
渲染:行動> '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> </p></div>
<div class="form_row">
<label for="mls">MLS Number:</label>
<%= text_field_tag 'mls', params[:mls] %>
</div>
<div class="form_row">
<label for "county"><font color="#ff0000">*County:</font></label>
<%= select_tag "county", options_for_select(@counties), :multiple => true, :size => 6 %>
</div>
<div class="form_row">
<label for "acreage_range">Acreage:</label>
<%= 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>
<%= 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>
<%= 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>
,直到我添加」,搜索工作正常:多=>真實,:大小=> 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
我已努力使縣,acreage_range和PRICE_RANGE領域到多個選擇框多種方式,但不能讓任何方法能夠正常工作。任何幫助將不勝感激。
感謝,
這很好地工作,如果用戶輸入一些針對搜索領域,但如果默認的「全部」選項留下,再沒有被發現。 – Reido 2010-04-05 15:06:51
看到我編輯的答案:] – 2010-04-05 15:42:56
首先感謝您的幫助!但我仍然無法正常工作。如果我提交搜索只能選擇一個縣一切工作正常,產生的URL是: http://www.mydomain.com/public/land?mls=&county[]=Adams&landtype=&commit=Search 但是,如果我選擇一個縣,然後選擇ALL面積範圍內,產生的URL不起作用: http://www.mydomain.com/public/land?mls=&county[]=Adams&acreage_range[]=ALL&landtype=&commit=Search 搜索呢找不到任何屬性。它看起來應該,但沒有運氣。 – Reido 2010-04-05 19:26:24