开发者

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 #`


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" />


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜