NullVoxPopuli
NullVoxPopuli

Reputation: 65143

Ruby on Rails: How do I submit a form of checkboxes?

I have an object: @object with tons of boolean fields.

now. according to the HTML spec, if a checkbox isn't checked, the value isn't sent.

Which is a problem... cause I need that information.

How do I get around that? the api for f.check_box suggests fields_for... http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M002298

but I couldn't figure out the syntax for @object.is_admin

=\

currently, without changing anything: i have this:

<% form_for @object do |f| %>
        <td><%= f.check_box :allow_downloads, :label => false  %></td>
.
.
.

error : undefined methodto_i' for #`

Upvotes: 0

Views: 2023

Answers (4)

NullVoxPopuli
NullVoxPopuli

Reputation: 65143

The problem was that my was accessing the wrong controller. I moved my form to a proper view (made more sense for usability, anyway) and it works.

Upvotes: 0

Draiken
Draiken

Reputation: 3815

There is no '=' in the <% f.check_box. It should be <%= f.chec_box...

besides, if all fields are boolean, you can set them all to false, and only set true the ones passed through the request.

Upvotes: 0

vise
vise

Reputation: 13383

It's nothing you should be concerned about.

If you look at the check_box helper generated code you'll see that rails creates a hidden field which represents the unchecked value. When you check the box, it will take precedence over the hidden field, because it's declared after it:

<input type="hidden" />
<input type="checkbox" />

Upvotes: 1

John Topley
John Topley

Reputation: 115362

Are the automatically generated hidden fields for the checkboxes as described by that documentation not working for you?

Upvotes: 0

Related Questions