Method: ActionView::Helpers::FormHelper#check_box
- Defined in:
- lib/action_view/helpers/form_helper.rb
#check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
Returns a checkbox tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). It’s intended that method returns an integer and if that integer is above zero, then the checkbox is checked. Additional options on the input tag can be passed as a hash with options. The checked_value defaults to 1 while the default unchecked_value is set to 0 which is convenient for boolean values. Since HTTP standards say that unchecked checkboxes don’t post anything, we add a hidden value with the same name as the checkbox as a work around.
Examples
# Let's say that @post.validated? is 1:
check_box("post", "validated")
# => <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
# <input name="post[validated]" type="hidden" value="0" />
# Let's say that @puppy.gooddog is "no":
check_box("puppy", "gooddog", {}, "yes", "no")
# => <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]" value="yes" />
# <input name="puppy[gooddog]" type="hidden" value="no" />
check_box("eula", "accepted", { :class => 'eula_check' }, "yes", "no")
# => <input type="checkbox" class="eula_check" id="eula_accepted" name="eula[accepted]" value="yes" />
# <input name="eula[accepted]" type="hidden" value="no" />
470 471 472 |
# File 'lib/action_view/helpers/form_helper.rb', line 470 def check_box(object_name, method, = {}, checked_value = "1", unchecked_value = "0") InstanceTag.new(object_name, method, self, nil, .delete(:object)).to_check_box_tag(, checked_value, unchecked_value) end |