Forum: Ruby on Rails check_box_tag

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
80159acdf89580a20e8e835772b5bf82?d=identicon&s=25 doug (Guest)
on 2007-03-20 01:01
(Received via mailing list)
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
E0e0e58f6ab7368839e9c606249f9b36?d=identicon&s=25 Nelson Hsu (nelson)
on 2007-03-20 01:53
Check out this thread:

http://www.ruby-forum.com/topic/60783


---
Basedex - A Human-edited search engine for Ruby on Rails
http://blazingrails.basedex.com/index/8
80159acdf89580a20e8e835772b5bf82?d=identicon&s=25 doug (Guest)
on 2007-03-20 04:09
(Received via mailing list)
> Check out this thread:
>
> http://www.ruby-forum.com/topic/60783

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
2ac70084b87876db2d5875341e57e283?d=identicon&s=25 David Dumaresq (Guest)
on 2007-03-21 01:04
(Received via mailing list)
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
_______________________________
Information and Educational Technology
Kwantlen University College - 604-599-2120
"So powerful is the light of unity that it can illuminate the whole
earth." --Bahá'u'lláh



"doug" <ddjolley@gmail.com>
Sent by: rubyonrails-talk@googlegroups.com
19-03-2007 05:00 PM
Please respond to
rubyonrails-talk@googlegroups.com


To
"Ruby on Rails: Talk" <rubyonrails-talk@googlegroups.com>
cc

Subject
[Rails] 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
This topic is locked and can not be replied to.