2012-07-23 134 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应用程序创建简单和高级搜索表单