2012-08-06 52 views
0

我正在嘗試製作一個搜索頁面,用戶可以在其中搜索許多不同領域的項目記錄。我希望他們能夠在client,industry,start_date,end_datekeywords上搜索。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 

回答

1

我會修改start_date_dd下降到有一個自定義字段,然後添加jQuery來顯示/隱藏起始日期/ END_DATE領域。然後在您的控制器中,您可以查看是否start_date_dd是自定義字段,那麼您應該使用start_date/end_date字段。

其他評論:

我不會通過他們搜尋的內容直接到數據庫。我會首先運行類似以下內容:

user_supplied_start_date = Time.zone.parse(search_start_date) 

這樣,如果他們給你一個無效的日期(解析user_supplied_start_date ==無),你可以給他們,你是無法解析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 

這樣,你根據什麼他們送你搜索。

您只列出當前分配給項目的客戶端。用戶不應該能夠搜索所有客戶端,並看到客戶端沒有任何項目嗎?

作爲一般規則,我寧願輔助方法具有視圖代碼...

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

VS

# app/helpers/search_helper.rb 
def clients_for_select 
    Client.select("id, name").map{|c| [c.id, c.name]} 
end