ruby-on-railsselect2-railsfilterrific

Rails select2 with filterrific gem


I currently have a list of events that I want my users to be able to filter. Each event has a performer and I store the performers id for each event.

It isn't very user friendly for my users to be able to search by the user id as it won't mean much to them so I want them to be able to search by the performers name.

I had previously created a simple search form that used select2 rails gem and the code in my view would look something like this:

<%= select_tag(:performer, options_from_collection_for_select(@performer,
              :id, :profile_name, :selected => params[:performer]), :include_blank => true, id: "user", :class => "form-control")  %>

<script type="text/javascript">
$(document).ready(function() {
$('select#user').select2({
  allowClear: true,
  placeholder: "Choose a profile name",
});

});

This would allow my users to be able to search and select a name and it would find the relevant events that related to their user_id.

I now want to use the filterrifc gem to make the search update in realtime as the user is typing and to display the results but I cannot work out how to modify my code to work alongside the gem.

If I follow the example in the documentation for a simple text search, the scope in the model would look like this:

   scope :performer, lambda { |query|
where("LOWER(events.performer) LIKE ?", "%#{query}%")}

But I cannot work out how to combine the select2 gem with the filterrific gem to get the result that I want.


Solution

  • This seems to have done the trick.

    <%= f.select :performer, options_from_collection_for_select(@performer, :profile_name,:name), {:include_blank => true}, id: "user", class: "form-control", data: { placeholder: "Choose a performer" } %>