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 开发者_JAVA技巧like the cleanest solution.
Thanks - max
In Rails 4 you can use include_hidden: false
example f.check_box :some_field, include_hidden: false
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
use <%= check_box_tag "your_model[your_field]" %>
f.check_box
always gives you a hidden field.
So this is interesting. check_box takes four arguments as the method definition shows:
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render
end
The checked value and unchecked value must be the third and fourth argument. By setting an unchecked_value that evaluates to a truthy value, then the hidden input will appear.
My initial goal was to have a visible checkbox with a value of true, and a hidden checkbox with a value of false. So I tried doing this:
<%= f.check_box :share, { }, true, false %>
However, because I actually passed in a boolean value for unchecked value, it made the hidden field not appear at all! I tracked it down to this line of code in the Rails source:
def hidden_field_for_checkbox(options)
@unchecked_value ? tag("input", options.slice("name", "disabled", "form").merge!("type" => "hidden", "value" => @unchecked_value)) : "".html_safe
end
If @unchecked_value evaluates to false or nil, then no hidden input field would display. Now if you want to send true and false values rather than '1' or '0' using check_box helper, then you will need to wrap the booleans in strings:
<%= f.check_box :share, { }, 'true', 'false' %>
But if that's the case, you matters well just use the defaults of '1'
and '0'
for boolean values and let Rails handle it.
Rails stores booleans as TinyInt, at least for the MySQL database. Consequently, that '1' will get stored in the database as 1 and will be considered a true value.
精彩评论