【问题标题】:Rails multiple select box issue for search用于搜索的 Rails 多选框问题
【发布时间】:2010-04-01 19:35:09
【问题描述】:

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

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

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'

渲染 :action => '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 => true, :size => 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 字段设置为多个选择框,但无法让任何方法正常工作。任何帮助将不胜感激。

谢谢,

【问题讨论】:

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


    【解决方案1】:
    Try    
    
    unless params[:county].blank?
      where << "county IN (:county)"
    end
    
            OR
    
    unless params[:county].blank?
      where << "county IN (  #{params[:county].join(',')})"
    end
    

    【讨论】:

      【解决方案2】:

      试试

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

      编辑#1

      我相信现在即使选择“全部”选项它也会起作用

      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
      

      希望它现在可以工作:]

      【讨论】:

      • 如果用户为该搜索字段输入一些内容,这会很好地工作,但如果保留默认的“全部”选项,则找不到任何内容。
      • 首先感谢您的帮助!但我仍然无法让它正常工作。如果我提交搜索只选择一个县,一切正常,结果 URL 是:mydomain.com/public/… 但是,如果我选择一个县,然后选择 ALL acreage range,结果 URL 不起作用:mydomain.com/public/… 搜索未找到任何属性。看起来应该,但没有运气。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      • 2016-07-11
      相关资源
      最近更新 更多