Rails: Implementing a search form for compound filtering
I have a model containing products. I would like to create a search form to allow users to apply compound filters to products as required. For example:
- Products with a price of between '10' (text field) and '50' (text field) with a colour of 'Red', 'Green' or 'Blue' (check box fields) and a weight of 'Less than' (select field) '5kg' (text field).
There will be a considerable number of fields that a user can filter by -- some, all, or none of them may be specified.
I am aware that the above can be achieved by using named scopes, lots of conditional logic (ca开发者_如何学编程se and if statements) and Rails form helpers, but is there a cleaner and more efficient way? Perhaps there is a gem/plugin to provide this functionality that I have overlooked?
Any advice much appreciated.
Many thanks.
I recommend the plugin searchlogic:
- Original plugin: binarylogic/searchlogic
- My fork, with support to sorting by polymorphic association fields: nandalopes/searchlogic
In the end, I went with meta_search, which seems to offer most of the functionality of searchlogic, but with Rails 3 support.
精彩评论