我正在嘗試製作一個搜索頁面,用戶可以在其中搜索許多不同領域的項目記錄。我希望他們能夠在client
,industry
,start_date
,end_date
和keywords
上搜索。Ruby on Rails:搜索時的優先領域
目前,該頁面顯示「客戶」和「行業」的下拉菜單以及「關鍵字」的文本框,因此用戶可以從數據庫中進行選擇並搜索關鍵字。
我在搜索開始日期時遇到了問題,因爲我試圖讓用戶選擇在某個時間範圍內(即3天前,1周前)搜索「開始日期」或者通過使用jquerys datepicker放入「開始日期」和「結束日期」來搜索特定日期。
我希望它將具體日期作爲優先事項,使用戶選擇時間比例和具體日期。
這裏是我的搜索查看:
<h1>Search</h1>
<% if @project_search.total_entries > 0 %>
<%= form_tag search_path, method: :get do %>
Client :
<%= select(@projects, :client, Project.all.map {|p| [p.client]}.uniq, :prompt => "-Any-", :selected => params[:client]) %></br>
Industry :
<%= select(@projects, :industry, Project.all.map {|p| [p.industry]}.uniq, :prompt => "-Any-", :selected => params[:industry]) %></br>
Select start date:
<%= select_tag "start_date_dd", options_for_select({
"Select a period" => "",
"3 days ago" => DateTime.now.to_date - 3.days, # = 259_200 sec.
"1 week ago" => DateTime.now.to_date - 1.week, # = 604_800 sec.
"1 month ago" => DateTime.now.to_date - 1.month, # = 2_592_000 sec.
"6 months ago" => DateTime.now.to_date - 6.months, # = 15_552_000 sec.
"1 year ago" => DateTime.now.to_date - 1.year, # = 31_557_600 sec.
}, :selected=>params[:start_date_dd])%>
or select project dates between
<%= text_field_tag ("start_date") %>
and
<%= text_field_tag("end_date") %></br>
Keywords :
<%= text_field_tag :keywords, params[:keywords] %></br>
<%= submit_tag "Search", name: nil %>
<% end %>
這裏是我的項目模型,沒有在搜索的特定日期選項的作用:
class Project < ActiveRecord::Base
attr_accessible :client, :end_date, :industry, :keywords, :start_date,
def self.search(search_client, search_industry, search_start_date_dd, search_keywords)
return scoped unless search_client.present? || search_industry.present? || search_start_date_dd.present? || search_keywords.present?
where(['client LIKE ? AND industry LIKE ? AND DATE(start_date) BETWEEN ? AND ? AND keywords LIKE ?',
"%#{search_client}%", "%#{search_industry}%" ,
search_start_date_dd, DateTime.now.to_date, "%#{search_keywords}%"
])
end
def self.paginated_for_index(projects_per_page, current_page)
paginate(:per_page => projects_per_page, :page => current_page)
end
end
下面是從項目負責人,我的搜索行動,其只考慮時間表搜索。
def search
@search = params[:client], params[:industry], params[:start_date_dd], params[:keywords]
@project_search = Project.search(*@search).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page)
@search_performed = [email protected]! { |c| c.blank? }.empty?
@project = Project.new(params[:project])
respond_to do |format|
format.html # search.html.erb
format.json { render :json => @project }
end
end
我曾試圖包括具體的日期搜索,但沒有運氣。希望有人能指出他們正確的方向。我是一個鐵軌小菜,所以請容易對我。提前致謝。
編輯:這是我試圖讓它工作。
def self.search(search_client, search_industry, search_start_date_dd, search_start_date, search_end_date, search_keywords)
return scoped unless search_client.present? || search_industry.present? || search_start_date_dd.present? || search_start_date.present? || search_end_date.present? || search_keywords.present?
where(['client LIKE ? AND industry LIKE ? AND DATE(start_date) BETWEEN ? AND ? OR DATE(start_date) BETWEEN ? AND ? AND DATE(end_date) BETWEEN ? AND ? AND keywords LIKE ?',
"%#{search_client}%", "%#{search_industry}%" , search_start_date_dd, DateTime.now.to_date, search_start_date, search_end_date, search_start_date, search_end_date,"%#{search_keywords}%"
])
end