0
我仍然在学习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页:
<%= form_tag('list', :method => 'get') do %>
<p>
<%= text_field_tag 'bedrooms', (params[:bedrooms]) %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
如何添加一个搜索栏浴室,另一个是最低价格,另一个是最大的价格,另一个用于拉链等?谢谢,亚当
我得到的语法错误时,试图将它添加到控制器:
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
是添加lambda表达式固定在上面的语法错误,但现在它好像@properties不返回数组,因为我收到以下错误消息:
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
正确的我明白了,不知道你可以继续重新定义它的下一行@properties和增加更多的条件吧,以后会尽量让你知道,非常感谢您的回答 – railsy
见以上为我得到的错误 – railsy
sry,修正)忘了添加逗号和lambdas –