I am using Ransack to create a search form on a model called Car. I have several boolean attributes on Car and I have included checkboxes in my search form for each one of them, for example:
<%= f.check_box :car_spec_anti_lock_brakes_true %>
<%= f.label :car_spec_anti_lock_brakes_true, "ABS" %>
When this checkbox is checked, the search results include all records where car_spec_anti_lock_brakes is true. When it is left unchecked, the results only return records where car_spec_anti_lock_brakes is false. How can I implement the search form so that when the checkbox is left unchecked all records where car_spec_anti_lock_brakes is either true or false are returned. So when the checkbox is checked return all records that have the attribute, but when it is unchecked return all records, independent of whether the attribute is true or false.
I solved by editing view file.
Your code generates hidden form that provides false param.
<%= f.check_box :car_spec_anti_lock_brakes_true %>
#generated html code
<input type="hidden" value="0" name="q[car_spec_anti_lock_brakes_true]"></input>
<input id="q_car_spec_anti_lock_brakes_true" type="checkbox" value="1" name="q[car_spec_anti_lock_brakes_true]"></input>
So, I removed hidden form.
<%= f.check_box :car_spec_anti_lock_brakes_true, include_hidden: false %>
#generated html code
<input id="q_car_spec_anti_lock_brakes_true" name="q[car_spec_anti_lock_brakes_true]" value="1" type="checkbox"></input>
When the checkbox is left unchecked all records where car_spec_anti_lock_brakes is either true or false are returned.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With