我正在尝试创建一个搜索页面,用户可以在其中搜索许多不同领域的项目记录。我希望他们能够在client
、industry
、start_date
、end_date
和上进行搜索keywords
。
目前,该页面显示“客户”和“行业”的下拉菜单,以及“关键字”的文本框,因此用户可以从数据库中选择并搜索关键字。
我在搜索开始日期时遇到问题,因为我试图让用户选择在某个时间范围内搜索“开始日期”,即 3 天前、1 周前......在下拉菜单中,或者通过使用 jquery 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 = !@search.reject! { |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
我曾尝试包括特定日期搜索,但没有运气。希望有人能指出他们正确的方向。我是一个 Rails 菜鸟,所以请放轻松。提前致谢。
编辑:这是我让它工作的尝试。
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