korywka
korywka

Reputation: 7653

How to filter items by some value using filter form?

I have items, that have price, color, manufacturer, model and so on. I want users to filter items by price range, manufacturer, model at the same time. For example: show all cars with manufacturer "Ford", model "Mustang" and price from 1k to 10k.

For pagination results i want use will_paginate (and submit form with ajax). I am looking for some "first bricks" or "first steps" to solve my problem. If i miss some good new tutorials, pls, feel free to give links :)

Upvotes: 0

Views: 568

Answers (1)

Eric Sites
Eric Sites

Reputation: 1544

Do something like this with a where query in your controller:

@records = Stuff.select("stuffs.*")

@records = @records.where("model = ?", model) if filter_car
@records = @records.where("price >= ? and price <= ?", price) if filter_price
@records = @records.paginate(:page => params[:page], :per_page => params[:per_page])

Upvotes: 1

Related Questions