jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 124 forks source link

Filtering issue with using multiple checkboxes & multiple: true #136

Open Rubioli opened 7 years ago

Rubioli commented 7 years ago

In my application I have a Post model and when a post is submitted, they will always have a category.

My categories are news, humor, facts, celebrities, food etc

Currently users can sort posts based on one category at a time. I did it by following the demo application and docs:

// Post model
class Post < ActiveRecord::Base

  filterrific default_filter_params: { sorted_by: 'created_at_desc' },
              available_filters: [
                  :with_category
              ]
  scope :with_category, lambda { |query|
    # return nil  if query.blank?
    return nil  if 0 == flag # checkbox unchecked
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.
    num_or_conditions = 1
    where(
        terms.map {
          or_clauses = [
              "LOWER(posts.category) LIKE ?"
          ].join(' OR ')
          "(#{ or_clauses })"
        }.join(' AND '),
        *terms.map { |e| [e] * num_or_conditions }.flatten
    )
  }
/// other options
// My PostsController
def index
  # redirect_to dashboard_path if not_influencer?
  @filterrific = initialize_filterrific(Post.not_deleted, params[:filterrific],
      select_options: {
          sorted_by: Post.options_for_sorted_by
      }
  ) or return
  @posts = @filterrific.find.page(params[:page]).per(36)

  respond_to do |format|
    format.html
    format.js
  end
  rescue ActiveRecord::RecordNotFound => e
    puts "Had to reset filterrific params: #{ e.message }"
    redirect_to(reset_filterrific_url(format: :html)) and return
end

/// My view

= form_for_filterrific @filterrific do |f|
  .row
    .col-md-2
      .form-group.chardin_box{'data-position' => 'top', 'data-intro' => 'Filter by Category.' }
        %label Category
        = f.select :with_category, options_for_select( [ ['Celebrities', "celebrities"],['Humor', "humor"],['Creative', "creative"],['Facts', "facts"],['Science', 'science'], ['News', 'news'],['Food','food'],['Animals','animals'],['Miscellaneous','miscellaneous'] ]), {include_blank: 'Select Catgeory'}, { class: 'form-control' }

This works fine now. But what Im trying to do is, for instance user can select news & facts and get the results based on these categories.

I have tried with:

// In Post model
scope :with_category, lambda { |query|
  eturn nil  if 0 == query # checkbox unchecked
  where(category: [*query])
}

&

scope :with_category, lambda { |query|
  eturn nil  if 0 == query # checkbox unchecked
  where('posts.category == ?', query)
}
// My view
= form_for_filterrific @filterrific do |f|
    .row
      .col-md-12
        = f.label 'Celebrities'
        = f.check_box :with_category, {multiple: true}, "celebrities", false

        = f.label 'Humor'
        = f.check_box :with_category, {multiple: true}, "humor", false

        = f.label 'News'
        = f.check_box :with_category, {multiple: true}, "news", false

        = f.label 'Facts'
        = f.check_box :with_category, {multiple: true}, "facts", false
/// Other part

With this code when I select checkbox, I can see the filterrific_spinner But it doesn't filter the posts based on them and I still see the same posts.

Using filterrific 2.1.2 from https://github.com/ayaman/filterrific.git (at master)

Any help is very appreciated 👍

k4m1113 commented 7 years ago

For future reference issues like this are better raised on StackOverflow as GitHub Issues are for tracking bugs. Without seeing all your code my initial sense is that you don't have the entire view layout set up for updating via AJAX. I would make sure you have a

I would recommend starting from the boilerplate code to get it working in your environment, and then adding your desired features from there.