2012-07-25 93 views
0

我試圖隱藏我的搜索結果,直到搜索發生之後。在我從主頁點擊搜索頁面的那一刻,該頁面顯示搜索框,同時還顯示了表格中的所有數據庫。我只想顯示搜索選項,然後搜索按鈕,然後根據搜索結果顯示正確的表格。Ruby on Rails:在搜索後在頁面上顯示結果

這裏是我的搜索視圖:

<h1>Search</h1> 

<%= 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> 

Role : 
<%= select(@projects, :role, Project.all.map {|p| [p.role]}.uniq, :prompt => "-Any-", :selected => params[:role]) %></br> 

Technologies : 
<%= select(@projects, :tech, Project.all.map {|p| [p.tech]}.uniq, :prompt => "-Any-", :selected => params[:tech]) %></br> 

Business Division : 

<%= select(@projects, :business_div, Project.all.map {|p| [p.business_div]}.uniq, :prompt => "-Any-", :selected => params[:business_div]) %></br> 

Project Owner : 
<%= select(@projects, :project_owner, Project.all.map {|p| [p.project_owner]}.uniq, :prompt => "-Any-", :selected => params[:project_owner]) %></br> 

Exception PM 
<%= select(@projects, :exception_pm, Project.all.map {|p| [p.exception_pm]}.uniq, :prompt => "-Any-", :selected => params[:exception_pm]) %></br> 


Start Date : 

<%= text_field_tag("start_date") %></br> 


End Date : 

<%= text_field_tag("end_date") %></br> 


Status : 

<%= select(@projects, :status, Project.all.map {|p| [p.status]}.uniq, :prompt => "-Any-", :selected => params[:status]) %></br> 

Keywords : 

<%= text_field_tag :keywords, params[:keywords] %></br> 

<%= submit_tag "Search", name: nil %> 

<% end %> 


<% if @search %> 
    <h3><%[email protected]_search.total_entries%> results</h3> 
<% end %> 

<% if @search %> 
<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> 

<%# end %> 

<% @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><%= l t.start_date %></td> 
    <td><%= l 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> 

Results per page: <%= select_tag :per_page, options_for_select([10,20,50], params[:per_page].to_i), :onchange => "if(this.value){window.location='?per_page='+this.value;}" %> 

<br /> 
<%= hidden_field_tag :direction, params[:direction] %> 
<%= hidden_field_tag :sort, params[:sort] %> 
<%= hidden_field_tag :per_page, params[:per_page] %> 
<%= hidden_field_tag :page, params[:page] %> 
<%= will_paginate (@project_search) %> 


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

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

下面是搜索行動:

def search 


@search = params[:client], params[:industry], params[:role], params[:tech], params[:business_div], params[:project_owner], params[:exception_pm], params[:status], params[:start_date], params[:end_date], params[:keywords] 

@project_search = Project.search(@search) 

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

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

end 

,這裏是我的模型。

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_client, search_industry, search_role, search_tech, search_business_div, search_project_owner, search_exception_pm, search_status, search_start_date, search_end_date, search_keywords) 
    return scoped unless search_client.present? || search_industry.present? || search_role.present? || search_tech.present? || search_business_div.present? || search_project_owner.present? || search_exception_pm.present? || search_status.present? || search_start_date.present? || search_end_date.present? || search_keywords.present? 

if search_start_date != "" 
search_start_date = Date.parse(search_start_date).strftime("%Y-%m-%d") 
end 
if search_end_date != "" 
search_end_date = Date.parse(search_end_date).strftime("%Y-%m-%d") 
end 



    where(['client LIKE ? AND industry LIKE ? AND role LIKE ? AND tech LIKE ? AND business_div LIKE ? AND project_owner LIKE ? AND exception_pm LIKE ? AND status LIKE ? AND start_date LIKE ? AND end_date LIKE ? AND keywords LIKE ?', "%#{search_client}%", "%#{search_industry}%" , "%#{search_role}%" , "%#{search_tech}%" , "%#{search_business_div}%" , "%#{search_project_owner}%" , "%#{search_exception_pm}%" , "%#{search_status}%", "%#{search_start_date}%", "%#{search_end_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 

希望你能看到我試過的。我得到錯誤wrong number of arguments (1 for 11),因爲我沒有分割參數。但是當我使用*@search時,我得到了大量的錯誤。

我在控制器原來的代碼看起來像在此之前我去約試圖隱藏表:

@project_search = Project.search(params[:client], params[:industry], params[:role], params[:tech], params[:business_div], params[:project_owner], params[:exception_pm], params[:status], params[:start_date], params[:end_date], params[:keywords]).order(sort_column + ' ' + sort_direction).paginated_for_index(per_page, page

+0

什麼是代碼的結果是你」目前正在運行?錯誤?意外的輸出? – 2012-07-25 13:01:23

+0

錯誤的參數數量(1代表11) – Jazz 2012-07-25 13:02:31

+0

爲什麼不使用'form_for'將所有這些參數放入只有一個使用子哈希的散列? – YuriAlbuquerque 2012-07-25 13:16:42

回答

2

它傳遞給.search當你需要圖示的@search PARAMS告訴紅寶石擴大你的論點分隔參數數組傳遞給Project.search

@project_search = Project.search(*@search) 

Information on using splat

編輯(回答您的後續問題)

測試與

<% if @search %> 

是不夠的,因爲它,即使它只是將永遠有一個計算結果爲true值, []["", "", "", "", "", "", "", "", "", "", ""]。正確檢查此問題的一種方法是創建一個新的布爾實例變量,如果搜索篩選器中的所有字段都爲空,則爲true

# put this after you set @search in your action 
@search_performed = [email protected]! { |c| c.blank? }.empty? 

現在測試@search_performed在你看來與

<% if @search_performed %> 
+0

我不明白。爲什麼要用'*'來代替參數中的'params'?你可以舉一個「搜索」功能的例子嗎? – Dougui 2012-07-25 13:23:44

+0

OP表示錯誤是「錯誤的參數數目(1表示11)」,這意味着當'search'函數期待** 11 **時他傳遞1個參數。 '@ search'包含一個他傳遞給'Project.search'的參數數組。他打算做的是將'@ search'數組中的11個元素作爲獨立參數傳遞。這就是'*'進來的地方。 – deefour 2012-07-25 13:27:46

+0

當我使用* @搜索時會發生各種錯誤。我將編輯我的問題,以便更好地瞭解我想要做的事情。感謝您的幫助 – Jazz 2012-07-25 14:04:08

0

你是存儲陣列中@search和u必須使用*操作

@project_search = Project.search(*@search) 
+0

嘗試您的建議時,我收到了很多不同的錯誤。我編輯了我的OP,以便更好地瞭解我想要做什麼,以及出了什麼問題。謝謝 – Jazz 2012-07-25 14:33:33

相關問題