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 (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 ...
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 ...
Advanced Editor (the main feature): – Syntax highlight for 40 languages, and one custom highlighter, fully customizeable. – Autocomplete with popup list. – Autocorrect. – Bookmarks. – ...
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 ...
- 输入命令:`open-form_tbtagwin_form` - 调整各组件的位置 - **功能介绍**:虽然该方法可能已不再适用最新版本,但过去可用于自定义预览窗口布局。 #### 十二、文件不显示关闭符号“×” - **操作步骤**: - `...
- Search List (Project/Applications/Article) - Profile - Simple Profile - Advanced Profile - Account - Account Center - Account Settings - Result - Success - Failed - Exception - 403 - 404 -...
4. **Extra Search (extra_search.html)**:这里包含了一些高级搜索表单的示例,如多条件筛选、模糊查询等,能够帮助用户快速定位所需信息。 5. **Form Samples (form_samples.html)**:这个文件提供了多种表单布局...
- **form="advancedSearch"**:定义高级搜索表单。 #### 六、其他操作按钮 - **删除、全面编辑等按钮**:这些按钮的功能主要包括对数据进行删除、批量操作等。 - **代码示例**: ```html {* {elseif $button_...
- Semantic tags provide more meaningful information about the content they contain, which is crucial for search engines and assistive technologies. Using tags like `<article>` and `<section>` helps ...
- **Search Direction Determination:** Discusses advanced methods for determining the search direction, such as Newton's method and quasi-Newton methods. - **Engineering Applications:** Illustrates ...
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 ...
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 ...
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 ...
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 ...
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 ...