Check_box_tag is limited?

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

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] Forms in HTML documents

Regards,
Rimantas

http://rimantas.com/

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


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

On 4/4/06, Jeff J. [email protected] 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?

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
:confused:

Jeff

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 R.s idea and create my own helper
method.

Thank you all very much for the replies.

Jeff

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. :slight_smile:

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