`
CaiDeHen
  • 浏览: 94276 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

#111 Advanced Search Form

阅读更多
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.rb
def products
  @products ||= find_products
end

private

def find_products
  Product.find(:all, :conditions => conditions)
end

def keyword_conditions
  ["products.name LIKE ?", "%#{keywords}%"] unless keywords.blank?
end

def minimum_price_conditions
  ["products.price >= ?", minimum_price] unless minimum_price.blank?
end

def maximum_price_conditions
  ["products.price <= ?", maximum_price] unless maximum_price.blank?
end

def category_conditions
  ["products.category_id = ?", category_id] unless category_id.blank?
end

def conditions
  [conditions_clauses.join(' AND '), *conditions_options]
end

def conditions_clauses
  conditions_parts.map { |condition| condition.first }
end

def conditions_options
  conditions_parts.map { |condition| condition[1..-1] }.flatten
end

def conditions_parts
  private_methods(false).grep(/_conditions$/).map { |m| send(m) }.compact
end
分享到:
评论

相关推荐

    Advanced.PDF.Password.Recovery.PRO.v2.12

    Advanced PDF Password Recovery Pro (APDFPRP) is a program to decrypt protected Adobe Acrobat PDF files, which have "user" ("open") and/or "owner" ("security, "master") passwords set, and to recover ...

    Big.Data.in.Complex.Systems.Challenges.and.Opportunities.3319

    The material of this book will be useful for researchers and practitioners in the field of big data as well as advanced undergraduate and graduate students. Each of the 17 chapters in the book opens ...

    Code Edit Studio v1.1(最新版) 支持超过40种开发语言的超级代码

    Advanced Editor (the main feature): – Syntax highlight for 40 languages, and one custom highlighter, fully customizeable. – Autocomplete with popup list. – Autocorrect. – Bookmarks. – ...

    R for beginner

    They are written in the form `response ~ predictors`, where `response` is the dependent variable and `predictors` are the independent variables. ##### Generic Functions R uses generic functions to ...

    SlickEdit使用技巧整理.docx

    - 输入命令:`open-form_tbtagwin_form` - 调整各组件的位置 - **功能介绍**:虽然该方法可能已不再适用最新版本,但过去可用于自定义预览窗口布局。 #### 十二、文件不显示关闭符号“×” - **操作步骤**: - `...

    react-admin:免费的React Admin模板

    - Search List (Project/Applications/Article) - Profile - Simple Profile - Advanced Profile - Account - Account Center - Account Settings - Result - Success - Failed - Exception - 403 - 404 -...

    bootstrap win8 后台模板

    4. **Extra Search (extra_search.html)**:这里包含了一些高级搜索表单的示例,如多条件筛选、模糊查询等,能够帮助用户快速定位所需信息。 5. **Form Samples (form_samples.html)**:这个文件提供了多种表单布局...

    vTiger CRM 维护说明

    - **form="advancedSearch"**:定义高级搜索表单。 #### 六、其他操作按钮 - **删除、全面编辑等按钮**:这些按钮的功能主要包括对数据进行删除、批量操作等。 - **代码示例**: ```html {* {elseif $button_...

    HTML5 Mastery: Semantics, Standards, and Styling

    - Semantic tags provide more meaningful information about the content they contain, which is crucial for search engines and assistive technologies. Using tags like `&lt;article&gt;` and `&lt;section&gt;` helps ...

    Introduction_to_Optimum_Design.pdf

    - **Search Direction Determination:** Discusses advanced methods for determining the search direction, such as Newton's method and quasi-Newton methods. - **Engineering Applications:** Illustrates ...

    Dreamweaver CS4 黄金插件10-02

    40. DwZone Advanced Form Controls V1.3.8 For Adobe Dreamweaver 41. DwZone Advanced Image Gallery V1.2.8 For Adobe Dreamweaver 42. DwZone Advanced Mail Plug In V1.2.1 For Adobe Dreamweaver 43. DwZone ...

    Dreamweaver CS4 黄金插件10-05

    40. DwZone Advanced Form Controls V1.3.8 For Adobe Dreamweaver 41. DwZone Advanced Image Gallery V1.2.8 For Adobe Dreamweaver 42. DwZone Advanced Mail Plug In V1.2.1 For Adobe Dreamweaver 43. DwZone ...

    Dreamweaver CS4 黄金插件10-1

    40. DwZone Advanced Form Controls V1.3.8 For Adobe Dreamweaver 41. DwZone Advanced Image Gallery V1.2.8 For Adobe Dreamweaver 42. DwZone Advanced Mail Plug In V1.2.1 For Adobe Dreamweaver 43. DwZone ...

    Dreamweaver CS4 黄金插件10-03

    40. DwZone Advanced Form Controls V1.3.8 For Adobe Dreamweaver 41. DwZone Advanced Image Gallery V1.2.8 For Adobe Dreamweaver 42. DwZone Advanced Mail Plug In V1.2.1 For Adobe Dreamweaver 43. DwZone ...

    Dreamweaver CS4 黄金插件10-04

    40. DwZone Advanced Form Controls V1.3.8 For Adobe Dreamweaver 41. DwZone Advanced Image Gallery V1.2.8 For Adobe Dreamweaver 42. DwZone Advanced Mail Plug In V1.2.1 For Adobe Dreamweaver 43. DwZone ...

Global site tag (gtag.js) - Google Analytics