I\'m having trouble understanding the best way to make a advanced search form. I have had a good search on the internet, looking at some ways, but I can\'t get them to work,
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...
I've been using MetaSearch in my application and found it quite convenient. If you've already considered it, what problems did you have?
There's also Ransack by the same author, it's a successor to MetaSearch.
A simple explanation can be found in this rails cast
Basically, we have to test if the params contain a specific field and create the filter. See the example below:
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
An alternative is Ransack. Ransack enables the creation of both simple and advanced search forms for your Ruby on Rails application