instance method
check_box
Ruby on Rails 2.2.3
Since v2.2.3Signature
check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
Returns a checkbox tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). This object must be an instance object (@object) and not a local 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.
Gotcha
The HTML specification says unchecked check boxes are not successful, and thus web browsers do not send them. Unfortunately this introduces a gotcha: if an Invoice model has a paid flag, and in the form that edits a paid invoice the user unchecks its check box, no paid parameter is sent. So, any mass-assignment idiom like
@invoice.update_attributes(params[:invoice])
wouldn’t update the flag.
To prevent this the helper generates a hidden field with the same name as the checkbox after the very check box. So, the client either sends only the hidden field (representing the check box is unchecked), or both fields. Since the HTML specification says key/value pairs have to be sent in the same order they appear in the form and Rails parameters extraction always gets the first occurrence of any given key, that works in ordinary forms.
Unfortunately that workaround does not work when the check box goes within an array-like parameter, as in
<% fields_for "project[invoice_attributes][]", invoice, :index => nil do |form| %> <%= form.check_box :paid %> ... <% end %>
because parameter name repetition is precisely what Rails seeks to distinguish the elements of the array.
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" />
Parameters
-
object_namereq -
methodreq -
optionsopt = {} -
checked_valueopt = "1" -
unchecked_valueopt = "0"
Source
# File actionpack/lib/action_view/helpers/form_helper.rb, line 495
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
InstanceTag.new(object_name, method, self, options.delete(:object)).to_check_box_tag(options, checked_value, unchecked_value)
end
Defined in actionpack/lib/action_view/helpers/form_helper.rb line 495
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::FormHelper