2012-07-23 135 views
5

我無法理解制作高級搜索表單的最佳方式。我在互聯網上進行了很好的搜索,在某些方面看,但我無法讓他們工作,因爲大多數建議都過時了。我已經提出了一個問題,但我認爲我太具體,我無法解決我的問題。我想在不同的文本框上搜索並用一個搜索按鈕下拉框。Ruby on Rails:高級搜索

EDIT2:

projects_controller:

def index 
    @projects = Project.all 

respond_to do |format| 
     format.html # index.html.erb 
     format.json { render :json => @projects } 
    end 
    end 

def search 
@project_search = Project.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 2, :page => params[:page]) 


end 




    # GET /projects/1 
    # GET /projects/1.json 
    def show 
    @project = Project.find(params[:id]) 

    respond_to do |format| 
     format.html # show.html.erb 
     format.json { render json: @project } 
    end 
    end 

    # GET /projects/new 
    # GET /projects/new.json 
    def new 
    @project = Project.new 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @project } 
    end 
    end 

    # GET /projects/1/edit 
    def edit 
    @project = Project.find(params[:id]) 
    end 

    # POST /projects 
    # POST /projects.json 
    def create 

    @project = Project.new(params[:project]) 


    @project.client = params[:new_client] unless params[:new_client].blank? 
    @project.exception_pm = params[:new_exception_pm] unless params[:new_exception_pm].blank? 
    @project.project_owner = params[:new_project_owner] unless params[:new_project_owner].blank? 
    @project.role = params[:new_role] unless params[:new_role].blank? 
    @project.industry = params[:new_industry] unless params[:new_industry].blank? 
    @project.business_div = params[:new_business_div] unless params[:new_business_div].blank? 

    respond_to do |format| 
     if @project.save 
     format.html { redirect_to @project, notice: 'Project was successfully created.' } 
     format.json { render json: @project, status: :created, location: @project } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @project.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PUT /projects/1 
    # PUT /projects/1.json 
    def update 
    @project = Project.find(params[:id]) 

    respond_to do |format| 
     if @project.update_attributes(params[:project]) 
     format.html { redirect_to @project, notice: 'Project was successfully updated.' } 
     format.json { head :no_content } 
     else 
     format.html { render action: "edit" } 
     format.json { render json: @project.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /projects/1 
    # DELETE /projects/1.json 
    def destroy 
    @project = Project.find(params[:id]) 
    @project.destroy 

    respond_to do |format| 
     format.html { redirect_to projects_url } 
     format.json { head :no_content } 
    end 
    end 




private 

    helper_method :sort_column, :sort_direction 
    def sort_column 
    Project.column_names.include?(params[:sort]) ? params[:sort] : "project_name" 
    end 

    def sort_direction 
    %w[asc desc].include?(params[:direction]) ? params[:direction] : "asc" 
    end 
end 

搜索查看:

<h1>Search</h1> 

<%= form_tag search_path, method: :get do %> 
<%= hidden_field_tag :direction, params[:direction] %> 
    <%= hidden_field_tag :sort, params[:sort] %> 
    <%= text_field_tag :project_name, params[:project_name] %> 
    <%= text_field_tag :client, params[:client] %> 
    <%= submit_tag "Search", name: nil %> 
<% end %> 



<table class = "pretty"> 
<table border="1"> 
    <tr> 
    <th><%= sortable "project_name", "Project name" %> </th> 
    <th><%= sortable "client", "Client" %></th> 
    <th>Exception pm</th> 
    <th>Project owner</th> 
    <th>Tech</th> 
    <th>Role</th> 
    <th>Industry</th> 
    <th>Financials</th> 
    <th>Business div</th> 
    <th>Status</th> 
    <th>Start date</th> 
    <th>End date</th> 
<% if false %> 
    <th>Entry date</th> 
    <th>Edited date</th> 
    <th>Summary</th> 
    <th>Lessons learned</tStackh> 
    <th>Customer benifits</th> 
    <th>Keywords</th> 
    <!th></th> 
    <!th></th> 
    <!th></th> 
<% end %> 
    </tr> 

<% @project_search.each do |t| %> 
    <tr> 
    <td><%= t.project_name %></td> 
    <td><%= t.client %></td> 
    <td><%= t.exception_pm %></td> 
    <td><%= t.project_owner %></td> 
    <td><%= t.tech %></td> 
    <td><%= t.role %></td> 
    <td><%= t.industry %></td> 
    <td><%= t.financials %></td> 
    <td><%= t.business_div %></td> 
    <td><%= t.status %></td> 
    <td><%= t.start_date %></td> 
    <td><%= t.end_date %></td> 
<% if false %> 
    <td><%= t.entry_date %></td> 
    <td><%= t.edited_date %></td> 
    <td><%= t.summary %></td> 
    <td><%= t.lessons_learned %></td> 
    <td><%= t.customer_benifits %></td> 
    <td><%= t.keywords %></td> 
<% end %> 
    <!td><%#= link_to 'Show', project %></td> 
    <!td><%#= link_to 'Edit', edit_project_path(project) %></td> 
    <!td><%#= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 
<%= will_paginate (@project_search) %> 


<%= button_to "Search Again?", search_path, :method => "get" %> 

<%# end %> 
<%= button_to "Home", projects_path, :method => "get" %> 

Project.rb

class Project < ActiveRecord::Base 
    attr_accessible :business_div, :client, :customer_benifits, :edited_date, :end_date, :entry_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech 

validates_presence_of :business_div, :client, :customer_benifits, :end_date, :exception_pm, :financials, :industry, :keywords, :lessons_learned, :project_name, :project_owner, :role, :start_date, :status, :summary, :tech 



def self.search search_term 
return scoped unless search_term.present? 
where find(:all, :conditions => ['project_name OR client LIKE ?', "%#{search_term}%"]) 
end 

end 

路線:

FinalApp::Application.routes.draw do 
resources :projects 
match "search" => "projects#search", :as => :search 
root :to => 'projects#index' 
end 

正如你所看到的,我離完成應用程序還有點距離。我正在嘗試製作搜索表單,以便能夠在以下字段中進行搜索:項目名稱,客戶端,ID,行業,角色,技術,項目所有者,狀態,開始日期,結束日期和關鍵字。搜索表單將具有文本框或下拉菜單,具體取決於用戶正在搜索哪個字段。我想要鏈接每個領域並一次搜索所有領域。之前,我只是使用project_name和客戶端作爲示例,以使您更容易理解我的代碼。希望你現在可以看到我正在嘗試做什麼。

+0

您看過這個[Railscast]嗎?(http://railscasts.com/episodes/111-advanced-search-form-revised )?這可能會幫助你。 – 2012-07-23 08:39:22

回答

8

您可以創建一個名爲search的新控制器。

搜索表單:

<%= form_tag search_index_path, method: :get do %> 
    <%= text_field_tag :project, params[:project] %> 
    <%= text_field_tag :client, params[:client] %> 
    <%= submit_tag "Search", name: nil %> 
<% end %> 

incude在你的routes.rb:

get "search/index" 

您的搜索控制器:

def index 
    #store all the projects that match the name searched 
    @projects = Project.where("name LIKE ? ", "%#{params[:project]}%") 
    #store all the clients that match the name searched  
    @clients = Client.where("name LIKE ? ", "%#{params[:client]}%") 
end 

現在你可以在@projects@clients玩索引視圖。

請注意,因爲如果搜索不匹配,這些變量可能會變爲零。

編輯 - 我假設你有兩個型號ProjectClient - 如果你不能創建一個新的控制器,你可以創建你的電流控制器搜索行動。

def search 
    #store all the projects that match the name searched 
    @projects = Project.where("name LIKE ? ", "%#{params[:project]}%") 
    #store all the clients that match the name searched  
    @clients = Client.where("name LIKE ? ", "%#{params[:client]}%") 
end 

而且比你可以使用@projects@clients在搜索視圖。

如果您嘗試在其他位置顯示結果(例如index視圖),則可以將上述操作移至正確的操作。

def index 
    .... 
    #store all the projects that match the name searched 
    @projects = Project.where("name LIKE ? ", "%#{params[:project]}%") 
    #store all the clients that match the name searched  
    @clients = Client.where("name LIKE ? ", "%#{params[:client]}%") 
end 

EDIT 2 - 好吧,你試圖通過域在同一個模型的組合來搜索:

你,改變你的搜索方法來添加這兩個領域:

def self.search(search_project, search_client) 
    return scoped unless search_project.present? || search_client.present? 
    where(['project_name LIKE ? AND client LIKE ?', "%#{search_project}%", "%#{search_client}%"]) 
end 

但是請注意,||將返回範圍,如果您的search_project或search_client不存在,您可以更改爲AND(& &),如果您願意。

另外,AND只會返回,如果兩者都匹配,我的意思是搜索的組合...你也可以改變它爲OR如果你想。

具有搜索表單:

搜索表單:

<%= form_tag search_index_path, method: :get do %> 
    <%= text_field_tag :project, params[:project] %> 
    <%= text_field_tag :client, params[:client] %> 
    <%= submit_tag "Search", name: nil %> 
<% end %> 

那麼你的控制器必須結合發送到模型:

@project_search = Project.search(params[:project], params[:client]).all 

我認爲這將解決這個問題。 ..

+0

嗨,Gabriel,我將搜索表單改爲您給我的建議,但是我保持路徑一致,因爲我正在努力創建一個新的搜索控制器。當我只有一個文本框時,我的應用程序開始工作。當然,添加另一個並不難,並且將搜索鏈接在一起? – Jazz 2012-07-23 12:26:55

+1

我正在看你以前的[問題](http://stackoverflow.com/questions/11577937/ruby-on-rails-search-form-multiple-search-fields),我不明白,如果你有一個或兩個楷模。建議的答案是試圖在Project模型中搜索客戶名稱,如果您有兩個模型,它將不起作用。 – gabrielhilal 2012-07-23 13:19:50

+0

對不起,我沒有說清楚。我只有一個模型,Project.rb。我將上傳我的控制器,搜索視圖,路線和模型文件,以便更好地瞭解我想要做的事情。 – Jazz 2012-07-23 13:49:49

1

我一直在使用MetaSearch在我的應用程序,發現它很方便。如果你已經考慮過了,你有什麼問題?

還有一個作者是Ransack,它是MetaSearch的繼承者。

0

一個簡單的解釋可以在這裏找到rails cast

基本上,我們必須測試params是否包含特定字段並創建過濾器。請參閱下面的示例:

def find_products 
    products = Product.order(:name) 
    products = products.where("name like ?", "%#{keywords}%") if keywords.present? 
    products = products.where(category_id: category_id) if category_id.present? 
    products = products.where("price >= ?", min_price) if min_price.present? 
    products = products.where("price <= ?", max_price) if max_price.present? 
    products 
end 

另一種方法是Ransack。 Ransack可以爲您的Ruby on Rails應用程序創建簡單和高級搜索表單