2

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 :)

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Rodrigo for the answer. But it didn't work. The same issue as before. Only one value in the certificates array. Here are the params after removing the simple form Parameters: {"utf8"=>"✓", "search_smart_listing"=>{"_"=>"1", "page"=>"", "per_page"=>"10"}, "search"=>{"certificates"=>["6"]}}
@Mahendhar I've updated my answer. You should still use 1 form, but also there's a problem with current release. Check my answer.
@Mahendhar Hey, Im trying to accomplish something similar where each checkbox will filter when checked. Currently my checkbox will not un-filter once i uncheck the checkbox and also if i checked more than one checkboxes it will not display the output of all selected checkboxes. Would you be able to guide me to the right. Thanks!!
@Tim it all depends on how you filter on your controller. Remember that unchecked boxes are not sent on form submit (only checked ones are), so you have to keep that in mind in order to make the right filter.
@Tim, This is my multiple checkboxes code in the view <%= hidden_field_tag 'certificates[]', '' %> <% Certificate.all.each do |certificate| %> <div class="checkbox"> <label class= "checkbox inline"> <%= check_box_tag 'certificates[]', certificate.id %> <%= certificate.title %> </label> </div> <% end %>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.