Ruby on Rails: Advanced search

You can create a new controller called search.

Your search form:

<%= 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 in your routes.rb:

get "search/index"

your search controller:

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

Now you can play with @projects and @clients in the index view.

Just be careful, because these variables might became nil if there is no match for the search.

EDIT – I am assuming you have two models Project and Client – if you cannot create a new controller you can create the search action in your current controller.

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

And than you can use the @projects and @clients in the search view.

If you are trying to display the results in somewhere else (for example index view), you can just move the above to the correct action.

 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 – OK, you are trying to search by a combination of fields in the same model:

You and change your search method to add these two fields:

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

But please note the || will return scope if your search_project OR search_client are not present, you can change for AND (&&) if you prefer.

Also, the AND will return only if both match, I mean the combination of search… You can also change it to OR if you want.

Having the search form:

Your search form:

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

Then your controller must send the combination to the model:

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

I think it will solve the problem…

Leave a Comment