I have two controller categories and products, relation is category has_many products in application.html.erb
catagory dropdown menu and form_tag
for search products. My requirement is when I selected any category name through dropdown menu and search any products, should show that categories product. if I will not select any category at that time of finding any product it should show products which is releated to all category.
application.html.erb
<select>
<option value>Catagory by Name</option>
<option value="Samsung">Samsung</option>
<option value="Nokia">Nokia</option>
<option value="Apple">Apple</option>
<option value="HTC">HTC</option>
<option value="Sony">Sony</option>
<option value="Blackbarry">Blackbarry</option>
</select>
<tr><%= form_tag products_path, :method => 'get' do %>
<p>
<%= text_field_tag :search,params[:search],placeholder: "Search Products"%>
<%= submit_tag "search",:name => nil %>
</p>
<% end %></tr>
product_controller.rb
def index
@products = Product.all
if params[:search]
@products = Product.search(params[:search])
else
@products = Product.all
end
end
product.rb
def self.search(search)
where("name like ?", "%#{search}%")
end
So please give me idea how to solve it.