Forum: Ruby on Rails check_box_tag is limited?

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.
C728d93eb5205cf20cac0c2928b0ad62?d=identicon&s=25 Jeff Jones (rurounijones)
on 2006-04-04 16:47
Hello all.

Am I going totally nuts here and just want to check (Natch!) something.
A normal check_box has two value posssibilities. One for checked and one
for unchecked, the value gets submitted with a form.

The check_box_tag on the other hand, has a checked_value (Called just
'value') and for the unchecked value...nothing, zip, nada.

This means that if you submit a form with a check_box_tag in it and it
is unchecked there is absolutely nothing in the params, not :checkbox =>
0...Am I the only one that is having trouble with this?

In my controllers I have a method that can be called from two different
places. One is from a form and another is from a link in another view.
My method is currently setup to behave differently depending on the
value of the checkbox.

I store the value in a session for future use (since it is a
semi-persistant value). The idea is to set the value of the checkbox
using the form and when the controller is called with
params[:checkbox].nil? it just uses the one stored in the session.

However since the check_box_tag is either value or nil I can't use this
method. Is it just me or does this make no sense? That the check_box_tag
has less functionality than the normal check_box. I am thinking of
modifying the taghelper code so that the tag matches the normal one but
I would rather not deviate from normal rails.

Am I missing something totally obvious or is check_box_tag limited
compared
to the proper check_box helper?

Jeff
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2006-04-04 17:07
(Received via mailing list)
> Am I going totally nuts here and just want to check (Natch!) something.
> A normal check_box has two value posssibilities. One for checked and one
> for unchecked, the value gets submitted with a form.
>
> The check_box_tag on the other hand, has a checked_value (Called just
> 'value') and for the unchecked value...nothing, zip, nada.
>
> This means that if you submit a form with a check_box_tag in it and it
> is unchecked there is absolutely nothing in the params, not :checkbox =>
> 0...Am I the only one that is having trouble with this?
<...>
> However since the check_box_tag is either value or nil I can't use this
> method. Is it just me or does this make no sense? That the check_box_tag
> has less functionality than the normal check_box. I am thinking of
> modifying the taghelper code so that the tag matches the normal one but
> I would rather not deviate from normal rails.
>
> Am I missing something totally obvious or is check_box_tag limited
> compared
> to the proper check_box helper?

Hi,

check_box_tag is not limited, it is check_box that is enhanced.

check_box_tag gives you what it should: <inputs type="checkbox" ...>
And by HTML spec[1] unchecked checkboxes cannot be 'successful',
i.e. their are not included in date browsers sends on submit.

check_box in addition to checkbox tag also generates hidden field with
the same
name as <input type="checkbox" ...> but with value to be sent when
checkbox is
unchecked.

Why don't you just use check_box where you need its functionality?

[1] http://www.w3.org/TR/html401/interact/forms.html#checkbox

Regards,
Rimantas
--
http://rimantas.com/
Bfc3ae865ab5cb781b9d1d4b50ab132b?d=identicon&s=25 David Rupp (Guest)
on 2006-04-04 17:10
(Received via mailing list)
Jeff,

check_box() handles this by adding a hidden field that defaults the
generated checkbox to its unchecked value. From the api docs
(api.rubyonrails.com):

check_box("post", "validated")

generates

<input type="checkbox" id="post_validate" name="post[validated]"
value="1" checked="checked" />
<input name="post[validated]" type="hidden" value="0" />

You could, as you suggest, extend the Rails code directly. Another
option would be to write your own helper method that calls
check_box_tag and appends the hidden field. That way you get the
functionality you need without having to worry about the consequences
of altering the Rails source code.

Hope this helps. Feel free to respond off-list if you'd like to
pursue this further.

David
E025e426244a038645a4a08ba1a2d97f?d=identicon&s=25 Anthony Moralez (Guest)
on 2006-04-04 17:17
(Received via mailing list)
On 4/4/06, Jeff Jones <rurounijones@hotmail.com> wrote:
> However since the check_box_tag is either value or nil I can't use this
> method. Is it just me or does this make no sense? That the check_box_tag
> has less functionality than the normal check_box. I am thinking of
> modifying the taghelper code so that the tag matches the normal one but
> I would rather not deviate from normal rails.
>
> Am I missing something totally obvious or is check_box_tag limited
> compared
> to the proper check_box helper?

What is stopping you from using check_box?
C728d93eb5205cf20cac0c2928b0ad62?d=identicon&s=25 Jeff Jones (rurounijones)
on 2006-04-04 19:24
> What is stopping you from using check_box?

My check_box_tag is currently being used to set a session variable to 1
or 0(which can change occasionally). I also want to be able to set if
the checkbox is checked or not upon loading the form.

The view code is currently

<%= check_box_tag('version', value = '1', checked = ischecked?, { :class
=> 'checkbox'} ) -%>

(ischecked is a helper method that returns true / false based on what
the session holds)

The controller code is currently:

session[:version] = params[:version] || 0

What I ideally want it to be is

session[:version] = params[:version] unless session[:version].nil?

This means that a request that doesn't explicitely set the version uses
the one that is currently in the session.

However from what I have read of the API the check_box is designed to
set values of object attributes which this isn't doing. It also appears
not to be able to be checked upon form load.

I assumed it would be easier to bodge check_box_tag to work for me than
check_box.

I am still a noob ( The API docs are still a place of constant learning
) so I am not sure if that explanation is detailed enough. If anyone can
explain how to make check_box work for me then I am all ears (eyes?). If
not then I will take up David Rupps idea and create my own helper
method.

Thank you all very much for the replies.

Jeff
C728d93eb5205cf20cac0c2928b0ad62?d=identicon&s=25 Jeff Jones (rurounijones)
on 2006-04-04 19:26
> What I ideally want it to be is
>
> session[:version] = params[:version] unless session[:version].nil?

Typo! that should ready


What I ideally want it to be is

session[:version] = params[:version] unless params[:version].nil?

Why do you always spot typos right AFTER posting and not when checking
:/

Jeff
Bfc3ae865ab5cb781b9d1d4b50ab132b?d=identicon&s=25 David Rupp (Guest)
on 2006-04-04 19:38
(Received via mailing list)
Idiomatically, this could be written as:

session[:version] ||= params[:version]

In English: "session[:value] equals itself, or params[:version] if it
exists". Same result, less typing. :-)

BTW, the two characters before the "=" are the logical OR operator
(two vertical bars); just wanted to mention that in case your mail
font doesn't make that clear.

Cheers,
David
This topic is locked and can not be replied to.