I am using Smart listing gem for real time filtering. The following is a simple form without a submit and url.
<%= smart_listing_controls_for(:search) do %>
<%= simple_form_for :search do |f| %>
<%= f.input :certificates, collection: Certificate.all, :as => :check_boxes, include_hidden: false, :input_html => {:multiple => true} %>
<% end>
<% end>
The above code generates multiple check-boxes with 'Certificate id's as values. As soon as one of the check-boxes is checked, smart listing sends a request to the controller with the params.
Parameters: {"utf8"=>"✓", "search_smart_listing"=>{"_"=>"1", "page"=>"", "per_page"=>"10"}, "authenticity_token"=>"z25rULU5JeeWcEZdpsy0+bz7OJFDWPmXrVGnzPvdG0cjM0ufpc3ydB9+5GywDQkUmcm6RGJnF0C4Yrd0sWpJ6g==", "search"=>{ "certificates"=>["6"]}}
The problem is when I select multiple check-boxes, the certificates array just has the latest value and not all the selected check-boxes values.
Also, when a check-box is selected and de-selected, the certificates array value in the params remains the same. I want the value to be removed from the certificates array in the params if the check-box is deselected and only want the certificates array to just have all the selected check-boxes values.
The following is the html code generated for one of the multiple check-boxes.
<span class="checkbox">
<label for="search_certificates_5">
<input class="check_boxes required" type="checkbox" value="5" name="search[certificates][]" id="search_certificates_5">
Certificate 1
</label>
</span>
Thanks in advance :)
Since both smart_listing_controls_for
and simple_form_for
create a form, one problem you might have is that you are creating a form inside a form, and that is nor recommended nor standard. That might lead to unexpected results.
Maybe try doing it without the simple_form helper, something like this (assuming Certificate
has a description attribute):
<%= smart_listing_controls_for(:search) do %>
<%= Certificate.all.each do |certificate| %>
<div class="checkbox">
<label class= "checkbox inline">
<%= check_box_tag 'search[certificates][]', certificate.id %>
<%= certificate.description %>
</label>
</div>
<% end %>
<% end %>
Update
Also, there's a problem with current release (v1.1.2) of the smart listing gem that doesn't allow to work with array inputs. Problem is in this part of the javascript code. This is fixed on current master branch and was updated recently on latest commit as you can see here.
To solve this use the latest commit in your Gemfile like this:
gem 'smart_listing', :git => 'https://github.com/Sology/smart_listing.git', :ref => '79adeba'
bundle install
after updating Gemfile and try the above code again.