Creating Checkboxes

Checkboxes are mostly simple. They can be checked or not checked, and Rails maps their contents to a boolean value transparently. This simple request for a checkbox:

<%= f.check_box :can_send_email %>

yields this bit of HTML:

<input id="person_can_send_email" name="person[can_send_email]" type="checkbox"
value="1" /><input name="person[can_send_email]" type="hidden" value="0" />

That’s a little more complicated than expected, though. Why is there a second input element of type hidden? It’s another Rails workaround, providing a default value in case the checkbox isn’t checked:

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 workaround.[1]

If the checkbox is checked, that value will go through. If not, the value of the hidden input with the same name will go through.

The check_box method has a few more tricks to offer. As was possible with the text fields, you can specify additional attributes—perhaps class for CSS styling?—with named parameters:

<%= f.check_box :can_send_email, :class => 'email' %>

This will produce a checkbox with a class attribute:

<input class="email" id="person_can_send_email" name="person[can_send_email]"
type="checkbox" value="1" /><input name="person[can_send_email]" type="hidden"
value="0" />

You can also specify that the box should be checked if you want, which will override the value that comes into the form from the underlying object. Use this with caution:

<%= f.check_box :can_send_email, {:class => 'email', :checked=>"checked"} %>

Notice that there are now curly braces around the arguments that specify attributes. They aren’t strictly necessary, but checkboxes allow for some additional arguments where they will be necessary, even if there is only one attribute given a value. More precisely, you can also specify return values in place of 1 and 0 if you’d like, if your code is set up to support them:

<%= f.check_box :can_send_email, {:class => 'email'}, "yes", "no" %>

This will generate:

<input class="email" id="person_can_send_email" name="person[can_send_email]"
type="checkbox" value="yes" /><input name="person[can_send_email]" type="hidden"
value="no" />

For most of the helper functions that create form components, the options hash is the last argument, and you can just list the named parameters for the attribute values at the end, without the braces around them. However, because checkboxes have the arguments for checked and unchecked values after the options hash, you need to specify the attributes in the middle, in curly braces, if you specify values for checked and unchecked. Ruby will give you strange errors if the braces are missing and the values appear at the end. (If you don’t specify values for checked and unchecked, you can just include named parameters without the braces as usual.)

Warning

If you’re using Rails’ built-in boolean type to store data from your checkboxes, don’t specify values for checked and unchecked. The default 1 and 0 are correct for this situation, and Rails won’t know what to do with other values (unless, of course, you provide code for processing them).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset