Dropdown menu for product search
I have many products with many categories that are related to has_many using membership.
I am trying to create a search box in which anyone can search for products and also filter their search with a dropdown of categories (so that only products with matching categories can be retrieved).
Think_sphinx index is in the product model I am not getting any errors but the dropdown does not affect the search.
MODEL:
has_many :memberships,:dependent=> :destroy
has_many :categories, :through => :memberships
named_scope :published, :conditions => {:publish => 1}
define_index do
indexes product_name
indexes product_description
indexes publish
indexes memberships.product_id
indexes memberships.category_id
indexes categories.category_name
end
end
CONTROLLER:
@products = Product.search params[:search],:conditions=>{@product.memberships.category_id =>params[:category_product] },:page=> params[:page] || 1,:per_page =>4
VIEW:
form_tag search_path, :method =>:get do
text_field_tag :search, params[:search]
form_tag categories_path, :method => :get do
select_tag"category", options_from_collection_for_select (Category.find (:all, :group=>:id), :id, :category_name,params[:category_product])
end
submit_tag "search", :name => nil
end
+2
sunil
source
to share
1 answer
You need to use the attribute for filtering. In your define_index use the "has" method, and in your search use: with parameters. Something like:
define_index do
…
has categories(:id), :as => categories_id
…
end
and the search will look like this:
Product.search params[:search], :with => { :categories_id => params[:category] }
+1
source to share