Search code examples
ruby-on-railssearchkick

Searchkick filtering by buttons


I have a 'feed' model, which has_many entries, these are all displayed on one page. Searchkick is all set-up and works like a charm with a default search bar, but unfortunately I can't seem to figure out how to do it with buttons instead.

I'm trying to use Searchkick to search via button clicks (Click Colour Brandy, get all results which match colour Brandy etc). I see in it's documentation this is called 'Aggregation', an example of this would be { color: "brandy" }.

Would I have to create individual methods for each of the filters within my controller/model? Then call them with a button click? How would I go about handling multiple?


Solution

  • No, you won't add new actions to your controller. Expanding on that aggregation example from Searchkick's doc:

    in the controller

    def filters
      allowed_filters = %w(color size)
      filters = params.select{|k, v| allowed_filters.include?(k) }
    end
    helper_method :filters
    

    in the view

    = link_to 'color brandy', {action: 'index', filters.merge(color: 'brandy'}, class: 'ui-button'