Rails multiple select box issue for search
Posted
by Reido
on Stack Overflow
See other posts from Stack Overflow
or by Reido
Published on 2010-04-01T19:35:09Z
Indexed on
2010/04/02
9:13 UTC
Read the original article
Hit count: 408
First off here is my model, controller, view:
My model, this is where I have my search code:---------------------------
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
My controller:----------------
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 => 'land_table' end
My View: ----------------------
<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>
The search works fine until I add ", :multiple => true, :size => 6" to make the county field multiple select. Then I get the error:
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
I've tried to make the county, acreage_range, and price_range fields into multiple select boxes numerous ways, but can not get any method to work correctly. Any help would be greatly appreciated.
Thanks,
© Stack Overflow or respective owner