Question

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?

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top