check_box_tag

It ppears that the check_box_tag inserts a name/value pair into params{} only when the checkbox is checked and inserts nothing when the checkbox is unchecked. This means that if one simply cycles through the keys in params{}, the names of fields associated with unchecked check boxes will be omitted. I was hoping that there was a way to get name/value pairs for unchecked check boxes with the value (presumably) being set to something that would indicate the unchecked condition. Any ideas on how I can work around this problem? Thanks for any input.

      ... doug

Check out this thread:

Check_box_tag is limited? - Rails - Ruby-Forum

Well, that's certainly squarely on point. I now understand that HTML was never designed to pass a value for an unchecked check box and that check_box is an enhanced version of check_box_tag that provides a work-around by passing the needed value as a hidden field with the same name as the checkbox when the checkbox is unchecked. This is sort of as I had anticipated, i.e., the Rails people have a way figured out to handle this.

So, the bottom line is that I'm happy to use check_box in lieu of check_box_tag. The problem is that in looking at the API, the syntax is a bit confusing. There appears to be more going on with check_box than just providing an enhanced version of check_box_tag to work around this problem. When I use check_box_tag, it works as it should (not as I want; but, as it should). Now what I need is to figure out how to take what I have and make it work with check_box so as to produce the result that I want. If anyone wanted to pass along an example or some guidance, it sure would be appreciated.

Thanks for the input.

      ... doug

Hi Doug,

I was hoping to give you a proof of concept when I read your post this morning. Well, such are the plans, eh… Anyway, my thought was:

You could create hidden fields for all your checkboxes with initial values. You may also need an instance variable array of these items, then in your action you could cycle through the array and get the name of the checkbox and check its value in params.

Something like (please check the syntax on all code):

controller-action: index

session[:check_boxes] = [a, b, c]

view template: index

<% form, :action=>‘create’ %>

<%= check_box_tag, a, :value=>off %>

<%= hidden_field, a, :value=>off %>

<%= check_box_tag, b, :value=>off %>

<%= hidden_field, b, :value=>off %>

<%= check_box_tag, c, :value=>off %>

<%= hidden_field, c, :value=>off %>

<% end %>

controller-action: create

@check_boxes = session[:check_boxes]

@on = 0

@off = 0

@check_boxes.each do |cb|

end

As I aluded to above, this is not tested. Now, what I’m not sure of is how the interaction will work between hidden fields and check_box field with identical names. There’s nothing wrong with it, you have the same thing in a radio-groups, for example. I’m just not sure which setting will ‘win out’.

Hope it gets you started. (Feel free to reply to my gmail dot com account under the name dfdumaresq)

Regards,

Dave