六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 119|回复: 0

#111 Advanced Search Form

[复制链接]

升级  20%

22

主题

22

主题

22

主题

秀才

Rank: 2

积分
80
 楼主| 发表于 2013-2-7 19:40:04 | 显示全部楼层 |阅读模式
If you need to create an advanced search with a lot of fields, it may not be ideal to use a GET request as I showed in episode 37. In this episode I will show you how to handle this by creating a Search resource.
<!-- views/searches/new.html.erb --><% form_for @search do |f| %>  <p>    <%= f.label :keywords %><br />    <%= f.text_field :keywords %>  </p>  <p>    <%= f.label :category_id %><br />    <%= f.collection_select :category_id, Category.all, :id, :name, :include_blank => true %>  </p>  <p>    Price Range<br />    <%= f.text_field :minimum_price, :size => 7 %> -    <%= f.text_field :maximum_price, :size => 7 %>  </p>  <p><%= f.submit "Submit" %></p><% end %># models/search.rbdef products  @products ||= find_productsendprivatedef find_products  Product.find(:all, :conditions => conditions)enddef keyword_conditions  ["products.name LIKE ?", "%#{keywords}%"] unless keywords.blank?enddef minimum_price_conditions  ["products.price >= ?", minimum_price] unless minimum_price.blank?enddef maximum_price_conditions  ["products.price <= ?", maximum_price] unless maximum_price.blank?enddef category_conditions  ["products.category_id = ?", category_id] unless category_id.blank?enddef conditions  [conditions_clauses.join(' AND '), *conditions_options]enddef conditions_clauses  conditions_parts.map { |condition| condition.first }enddef conditions_options  conditions_parts.map { |condition| condition[1..-1] }.flattenenddef conditions_parts  private_methods(false).grep(/_conditions$/).map { |m| send(m) }.compactend
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表