问题
I have a search page where a user can search for different projects, using different dropdown menus. They can search for more than one field at a time, and the search will return something if it finds a project that matches all fields.
Heres my search action in my project controller:
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
and here is my search view.
<%= 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>
and here is my 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
If I use debug, and search for the first technology in the technology drop down, I get this.
---
utf8: ✓
client: ''
industry: ''
technols:
id:
- ''
- '1'
keywords: ''
per_page: '10'
action: search
controller: projects
page: 1
but no results.
I have a new table that holds all the technologies that are related to a project, and I am having trouble setting up the search, to search on all fields, as well as searching to see if any of the technologies match with the ones in the Technol table.
Can anyone point me in the right direction. I am new to rails, so please remember this when trying to help.
Thanks in advance.
回答1:
Maybe you will try to use searching gems, like meta_search?
回答2:
Or try using external search engine like Sphinx, and thinking_sphinx gem
来源:https://stackoverflow.com/questions/12636413/ruby-on-rails-searching-two-models-in-one-search