我有一个搜索页面,用户可以使用不同的下拉菜单搜索不同的项目。他们一次可以搜索多个字段,如果找到与所有字段匹配的项目,搜索将返回一些内容。
这是我在项目控制器中的搜索操作:
def search
@search = params[:client], params[:industry], params[:tech], 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])
@all_technols = Technol.all
@project_technol = @project.projecttechnols.build
respond_to do |format|
format.html # search.html.erb
format.json { render :json => @project }
end
end
这是我的搜索视图。
<%= stylesheet_link_tag "search" %>
<body>
<div id = "search_op">
<%= form_tag search_path, method: :get do %>
<div class="client">
Client :
<%= select(@projects, :client, Project.order("client").map{|p| [p.client]}.uniq, :prompt => "-Any-", :selected => params[:client]) %></br>
</div>
<div class="industry">
Industry :
<%= select(@projects, :industry, Project.order("industry").map {|p| [p.industry]}.uniq, :prompt => "-Any-", :selected => params[:industry]) %></br>
</div>
<div class="tech">
Technologies :
<%#= select(@projects, :tech, Project.order("tech").map {|p| [p.tech]}.uniq, :prompt => "-Any-", :selected => params[:tech]) %></br>
<!/div>
<%= fields_for(@project_technol) do |ab| %>
<%= collection_select(:technols, :id, @all_technols, :id, :tech, {}, {:multiple => true } ) %>
</div>
<% end %>
<div class="keywords">
Keywords :
<%= text_field_tag :keywords, params[:keywords] %></br>
</div>
<div class="results">
Results per page: <%= select_tag :per_page, options_for_select([10,20,50], :selected=>params[:per_page]), { :onchange => "this.form.submit();"} %></br>
<%#Results per page: <%= select_tag :per_page, options_for_select([1,2,5], :selected=>params[:per_page]), :onchange => "'#{request.query_string}&per_page=' + this.getValue()" %></br>
</div>
<div class="buttons">
<div id="search_button">
<%= submit_tag "Search", name: nil, :class => "button" %>
</div>
<% end %>
<div class="home_button">
<%= button_to "Home", projects_path, :class => "button", :method => "get" %>
</div>
<div id="reset_button">
<%= button_to "Reset Search", search_path, :class => "button", :method => "get" %>
</div>
</div>
</div> <%#search_op div%>
<div class ="menu"></div>
<div id ="section3">
<% if @project_search.total_entries > 0 %>
<% if @search_performed %>
<table id = "mytable">
<tr>
<th><%= sortable "project_name", "Project name" %> </th>
<th><%= sortable "client", "Client" %></th>
<th><%= sortable "tech", "Technologies" %></th>
<th><%= sortable "industry", "Industry" %></th>
</tr>
<% @project_search.each do |t| %>
<tr>
<td><%= link_to t.project_name, t %></td>
<td><%= t.client %></td>
<td><%= t.tech %></td>
<td><%= t.industry %></td>
<!td><%#= link_to 'Show', t %></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>
<div id ="total_results"><%=@project_search.total_entries%> results</div>
<%end %>
<% else %>
<h2> Sorry, there are no results matching your search. Please try again. </h2>
<% end %>
<br />
<%# end %>
</div> <%# table div %>
</div> <%# section2 div %>
<% if @search_performed %>
<%= 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), :class => 'will' %>
<% end %>
</body>
</html>
这是我的project.rb:
class Project < ActiveRecord::Base
has_many :projecttechnols
has_many :technols, :through => :projecttechnols
def self.search(search_client, search_industry, search_tech, search_keywords)
return scoped unless search_client.present? || search_industry.present? || search_tech.present? || search_keywords.present?
where(['client LIKE ? AND industry LIKE ? AND tech LIKE ? keywords LIKE ?',
"%#{search_client}%", "%#{search_industry}%" , " "%#{search_tech}%"
"%#{search_keywords}%"
])
end
def self.paginated_for_index(projects_per_page, current_page)
paginate(:per_page => projects_per_page, :page => current_page)
end
end
如果我使用调试,并在技术下拉列表中搜索第一项技术,我会得到这个。
---
utf8: ✓
client: ''
industry: ''
technols:
id:
- ''
- '1'
keywords: ''
per_page: '10'
action: search
controller: projects
page: 1
但没有结果。
我有一个新表,其中包含与项目相关的所有技术,并且在设置搜索、搜索所有字段以及搜索是否有任何技术与技术表。
谁能指出我正确的方向。我是 Rails 新手,所以在尝试提供帮助时请记住这一点。
提前致谢。