rails 中的多个搜索字段 [英] Multiple search fields in rails

查看:34
本文介绍了rails 中的多个搜索字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在学习 RoR,我花了一整天时间才找到一个简单的搜索字段,该字段可按卧室数量过滤房产.它现在运行良好,但我不知道这是否是正确的方法,因为我不知道如何调整它以便我可以为浴室、最低价格、最高价格、拉链等添加额外的搜索字段.

I'm still learning RoR and its taken me all day to figure out a simple search field that filters properties by the number of bedrooms. It's working perfectly now but I don't know if it's the right way to do it cos I can't figure out how to adapt it so that I can add extra search fields for bathrooms, minimum price, max price, zip etc.

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

The search field and submit button and the results are on the list page so in the controller I have:

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 页面是:

and the list.html.erb page is:

<%= form_tag( 'list', :method => 'get') do %>
<p>
<%= text_field_tag 'bedrooms', (params[:bedrooms]) %>

<%= submit_tag "Search", :name => nil %>
</p>
<% end %>

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

How do I add a search field for bathrooms, another for minimum price, another for max price, another for zip etc? thanks, Adam

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

Am getting syntax errors when trying to add this to controller:

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 没有返回数组,因为我收到以下错误消息:

Yes adding the lambdas fixed the above syntax errors but now it's as if the @properties is not returning an array because I'm getting the following error message:

undefined method `each' for #<Class:0x007fd5235250f8>

Extracted source (around line #29):

26:       <th>Price</th>
27:     
28:     </tr>
29:     <% @properties.each do |property| %>
30:     <tr>
31:       
32:       <td><%= link_to(property.address, {:action => 'show', :id => property.id}) %></td>

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

Fixed this error message, I hadn't defined it properly in the controller, I had put @properties.all instead of @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

这篇关于rails 中的多个搜索字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆