0

我正在尝试创建一个搜索页面,用户可以在其中搜索许多不同领域的项目记录。我希望他们能够在clientindustrystart_dateend_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
4

1 回答 1

1

我将修改 start_date_dd 下拉列表以具有自定义字段,然后添加 jQuery 以显示/隐藏 start_date/end_date 字段。然后在您的控制器中,您可以查看 start_date_dd 是否是自定义字段,那么您应该使用 start_date/end_date 字段。

其他的建议:

我不会将他们搜索的内容直接传递给数据库。我会首先运行如下内容:

user_supplied_start_date = Time.zone.parse(search_start_date)

这样,如果他们给你一个无效的日期(解析的 user_supplied_start_date == nil),你可以给他们一个警告,告诉他们你无法解析 search_start_date,而是向他们显示最后_天。

我还看到了以下代码:

def self.search
  scoped = scoped.where(['client like %?%', params[:client]) if params[:client].present?
  scoped = scoped.where(['client like %?%', params[:industry]) if params[:industry].present?
  ...
  scoped
end

这样,您就可以根据他们发送给您的内容进行搜索。

您只列出当前分配给项目的客户。用户不应该能够搜索所有客户并看到_client没有任何项目吗?

作为一般规则,我更喜欢帮助方法而不是在视图中包含代码......

Project.all.map {|p| [p.client]}.uniq

对比

# app/helpers/search_helper.rb
def clients_for_select
  Client.select("id, name").map{|c| [c.id, c.name]}
end
于 2012-08-12T15:19:17.640 回答