简体   繁体   中英

searchlogic with checkboxes

I am using searchlogic to peform searches on my resultset.

Below is my search form which contains checkboxes.

<% form_for @search do |f| %>
  <div class="searchbox" style="text-align: left; width: 100%">

    <span> Status: </span>

    Incomplete - <%=   f.check_box :sub_status_fk_equals_any, {:name => "search[sub_status_fk_equals_any][]"}, "51" %> 
    Complete - <%=   f.check_box :sub_status_fk_equals_any, {:name => "search[sub_status_fk_equals_any][]"}, "52" %> 
    Under Review - <%=   f.check_box :sub_status_fk_equals_any, {:name => "search[sub_status_fk_equals_any][]"}, "53" %> 
    Approved - <%=   f.check_box :sub_status_fk_equals_any, {:name => "search[sub_status_fk_equals_any][]"}, "54" %> 

    <%= f.submit "Submit"%>
  </div>
<% end %>

The above code works fine as long as the checkboxes are selected but if all the checkboxes are empty, instead of returning all the rows, nothing is returned.

In my address bar, the following parameters are displayed:

http://localhost/sub_test/?
search[order]=descend_by_sub_entry_date&
search[sub_status_fk_equals_any][]=0&
search[sub_status_fk_equals_any][]=0&
search[sub_status_fk_equals_any][]=0&
search[sub_status_fk_equals_any][]=0&
commit.x=15&commit.y=18&commit=Search

It is passing 0 if none of the checkboxes is selected.

How do I return all the rows if nothing is selected?

Ok, found the solution. You need to pass 'nil' for the unchecked_value

Incomplete - <%=   f.check_box :sub_status_fk_equals_any, {:name => 
      "search[sub_status_fk_equals_any][]"}, "51", nil %> 

        Complete - <%=   f.check_box :sub_status_fk_equals_any, {:name => "search[sub_status_fk_equals_any][]"}, "52", nil %> 

        Under Review - <%=   f.check_box :sub_status_fk_equals_any, {:name => "search[sub_status_fk_equals_any][]"}, "53", nil %> 

        Approved - <%=   f.check_box :sub_status_fk_equals_any, {:name => "search[sub_status_fk_equals_any][]"}, "54", nil %> 

Hope this can be of help to someone else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM