Two or more Level Categorization

Hi Alex! I have watched your tutorial for Categorization. It works perfect. But I was wondering how can we implement one more level on top of the categorization. As an example, choose a jewellery category and then want to filter the price range or the colour option under that specific category.

Posted over 5 years ago by Alparslan Kapani
Posted about 5 years ago by Alex Yang

Hi Alparslan! We only need to make some simple modifications to build a multiple categorization feature.

If you know you only need two filters (like Yelp does with its keyword + location search), you could simply add a second search form into the navbar. Then in your controller, instead of checking if the single form field was blank, you'll need to modify it to check both form fields. So replace
if params[:category].blank?
with something like
if (params[:category].blank?) && (params[:color].blank?)

(Of course, you'll also need to decide what should happen when one search field is filled in while the other is blank.)

Then, in the case where both form fields are filled in, we'll need to modify our controller code by figuring out which color_id to use. Like with the category_id, we'll do that by adding the following line:
@color_id = Color.find_by(name: params[:color]).id

We'll also need to modify the search results that are displayed:
@listings = Listing.where(category_id: @category_id, color_id: @color_id)

That's it! Now if we want to generalize this to more than 2 filters, the main difference is that we won't be able to add many more search forms to our navbar (it would be too crowded). So you may want to have checkboxes (e.g. '$10 and under', '$10-20', etc) or take users to a separate page with advanced search options. Whatever you choose, you'll then need to modify your controller code to account for these extra search parameters. Hope this helps!

0
Posted about 5 years ago by Alparslan Kapani

Hi Alex, I have tried and created a gender search form as same as the category videos instructions. But when i select a specific category or gender nothing shows up. I m not sure about this code( @listings = Listing.where(category_id: @category_id, color_id: @color_id) ) I have tried a join method and a symbol like =|| but i missed something i thing.
And also the link is: /listings?gender=Unisex . It doesn t make a second query like /listings?gender=Unisex?category=furniture

0
Posted about 5 years ago by Alex Yang

Ah I think I know what the issue is. Is it possible that the two search boxes in your navbar are separate forms? You'll need to make them into a single form with two different input fields. This will ensure that both of your search terms are captured.

0