rails中的多个搜索字段

我还在学习RoR,它花了我一整天的时间来找出一个简单的搜索字段,按照卧室的数量过滤属性。 它现在工作得很好,但我不知道它是否是正确的方法,因为我无法弄清楚如何调整它,以便我可以为浴室添加额外的搜索字段,最低价格,最高价格,拉链等。

搜索字段和提交按钮以及结果都在列表页面上,所以在控制器中我有:

def list @properties = Property.bedrooms(params[:bedrooms]) end 

在我的模型中:

 def self.bedrooms(bedrooms) if bedrooms find(:all, :conditions => ["bedrooms LIKE ?", "%#{bedrooms}%"]) else find(:all) end 

结束

而list.html.erb页面是:

  'get') do %> 

nil %>

如何为浴室添加搜索字段,另一个为最低价格,另一个为最高价格,另一个为zip等? 谢谢,亚当

尝试将此添加到控制器时出现语法错误:

 scope :bedrooms, {|b| where("bedrooms LIKE ?", b)} scope :price_greater, {|p| where("price > ?", p)} 

错误是:

 SyntaxError in PropertiesController#list /Users/Adam/Documents/Websites/idx_app/app/models/property.rb:4: syntax error, unexpected '|', expecting '}' scope :bedrooms, {|b| where("bedrooms LIKE ?", b)} ^ /Users/Adam/Documents/Websites/idx_app/app/models/property.rb:4: syntax error, unexpected '}', expecting keyword_end scope :bedrooms, {|b| where("bedrooms LIKE ?", b)} ^ /Users/Adam/Documents/Websites/idx_app/app/models/property.rb:5: syntax error, unexpected '|', expecting '}' scope :price_greater, {|p| where("price > ?", p)} ^ /Users/Adam/Documents/Websites/idx_app/app/models/property.rb:5: syntax error, unexpected '}', expecting keyword_end 

是的添加lambdas修复了上面的语法错误,但现在好像@properties没有返回数组,因为我收到以下错误消息:

 undefined method `each' for # Extracted source (around line #29): 26: Price 27: 28:  29:  30:  31: 32:  'show', :id => property.id}) %> 

修复了这个错误信息,我没有在控制器中正确定义它,我放了@ properties.all而不是@properties = @ properties.all

通过使用范围来做到这一点……

  scope :bedrooms, lambda{ |b| where("bedrooms LIKE ?", b) } scope :price_greater, lambda{ |p| where("price > ?", p) } 

在控制器中

  @properties = Property.scoped @properties = @properties.bedrooms(params[:bedrooms]) if params[:bedrooms].present? @properties = @properties.price_greater(params[:min]) if params[:min].present? ..... @properties = @properties.paginate.... or just @properties.all