Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i tell the check_box method to NOT add a hidden check_box for the 'unchecked' value?

I'm using rails 2.3.4, which, when you call .check_box on the 'f' object in a form_for, makes a visible checkbox input for the 'checked' value, and a hidden checkbox input for the 'unchecked' value: http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M002434&name=check_box

The problem with this is that i have a validates_acceptance_of validation on the check_box, and if it's not checked, i'm getting a field_with_errors div wrapped around the visible checkbox AND the hidden checkbox, so that the error message appears twice.

In this instance i don't want a value passed through in the 'unchecked' case, so i don't want rails to add the hidden checkbox - this (switching off the hidden checkbox) would solve my problem. I can't figure out how to tell it to not add the hidden checkbox though. Can anyone tell me?

I know that i could get round this by making a check_box_tag, which doesn't add the hidden 'unchecked' case checkbox, but then i don't get field_with_errors stuff wrapped around the checkbox if it's not checked. Dispensing with the hidden field seems like the cleanest solution.

Thanks - max

like image 542
Max Williams Avatar asked Dec 15 '10 11:12

Max Williams


2 Answers

In Rails 4 you can use include_hidden: false

example f.check_box :some_field, include_hidden: false

like image 147
shlensky Avatar answered Sep 22 '22 13:09

shlensky


Since Rails 3.2, the hidden field will not be shown if the unchecked_value argument evaluates to false.

Example: f.check_box :your_field, {}, checked_value, false

See the Rails source: 3.2, 4.1

like image 36
Dan Rice Avatar answered Sep 22 '22 13:09

Dan Rice