mardi 19 avril 2016

Rails - Pass multiple checkbox values through params

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

Aucun commentaire:

Enregistrer un commentaire