Question

I am trying to setup searchlogic on nested resources. I have Category has_many :products also Category has_many :brands through :products

So structurally its Category/Brand/Product

As a user navigates the site they click a category, which uses the Category#show action.

#Category_controller
def show
  @category = Category.find_by_url_name(params[:id])
  @brands = @category.brands
  @categories = Category.find(:all)
  @meta_title = "#{@category.name}"

  respond_to do |format|
    format.html do |wants|
      @brand = @brands.first


      @products = @category.products.paginate(:conditions => {:brand_id => @brand}, :page => params[:page])
      render :template => 'brands/show'
    end
    format.xml  { render :xml => @category }
  end
end

So it renders the list of available brands for that Category, and also display the products in the first listed brand.

If the user then clicks a different brand from the list, the user is taken to the Brand#show action.

#Brands_controller
def show
  @category = Category.find_by_url_name(params[:category_id])
  @brand = Brand.find(params[:id])
  @search = Product.search(params[:search])
  @products = @search.paginate(:conditions => {:brand_id => @brand, :category_id => @category}, :page => params[:page])
  @meta_title = "#{@brand.name}"
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @brand }
  end

I have started implementing Searchlogic, gem installed, and some code in place. But when it searches it removes the category from the url http://localhost:3000/brands/14?search[order]=ascend_by_price While its not a major problem if it worked, it doesn't. I have breadcrumbs that need to have access to the relevant category and brand info...

So at the end of the day I am trying to learn how to implement Searchlogic on nested resources. Can anyone guide me along in that journey?

end
Was it helpful?

Solution

I was able to get this running on the category#show but still not Brand#show. the fix in Category#show was

`     def show
    @category = Category.find_by_url_name(params[:id])
    @brands = @category.brands
    @categories = Category.find(:all)
    @meta_title = "#{@category.name}"

  respond_to do |format|
    format.html do |wants|
      @brand = @brands.first

      @search = @category.products.search(params[:search])
      @products = @search.paginate(:conditions => {:brand_id => @brand}, :page => params[:page])
      render :template => 'brands/show'
    end
    format.xml  { render :xml => @category }
  end
end`

Brands still doesn't work right, any ideas?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top