Check_box for Noob

I have read and read and obvisouly I am not getting something. Basically
I am wanting to have a check box checked if the value of a database
field is set to 1. Here is my code.

<% for prod_detail in @product.prod_detail %>

Color:<%= prod_detail.color %> Size:<%= prod_detail.size %> Price:<%= prod_detail.price %> Available:

–this is always unchecked even though available is one in the database
<%= check_box (“available”, prod_detail.available) %>

–this actually works.
<input type=“checkbox” name=“available[<%= prod_detail.id %>]”
value="<%= prod_detail.available %>"
<% if prod_detail.available == 1 %> checked=“checked” <% end
%> />

<% end %>

So why does one work and the other does not?
Thanks in advance
Thom

Hi!

Try this:

<%= check_box (“available”, prod_detail.available?) %>

1 != true in ruby.

Markus

Markus Piff wrote:

On 9/22/06, Thom M. [email protected] wrote:

Size:<%= prod_detail.size %>

%> />

Actually, only ‘false’ and ‘nil’ are false in ruby. The reason
check_box isn’t working for you is because you aren’t using it
properly.

<%= check_box ‘prod_detail’, ‘available’ %>

should work, although it might be looking for an instance variable so
you may need to use @prod_detail instead.

_Kevin

_Kevin wrote:

Markus Piff wrote:

On 9/22/06, Thom M. [email protected] wrote:

Size:<%= prod_detail.size %>

%> />

Actually, only ‘false’ and ‘nil’ are false in ruby. The reason
check_box isn’t working for you is because you aren’t using it
properly.

<%= check_box ‘prod_detail’, ‘available’ %>

should work, although it might be looking for an instance variable so
you may need to use @prod_detail instead.

_Kevin

Well Kevin and Markus, I appreciate all you have done but neither
worked. Do you have any other suggestions?

Checkboxes in HTML are only trasnsmitted if they are set. For this
reason, most seriousapplications depending on accurate checkbox state
write their own checkbox idiom.

I use a cell in a table contraining a hidden input field, and set the
background image to eithe cbChecked.png or cbUnchecked.png based on teh
contents of the input field. The onclick event of the table cell is
hooked to javascript that sets or clears the hidden input field text.
The hidden input field is named in a manner that is parseable by Rails,
depending on the purpose of the application.

My checkbox idiom is:

function checkboxClick(element) {
inputValue = element.firstChild

if (inputValue.getAttribute ("value") == "off")
{
  inputValue.setAttribute ("value", "on")
  element.setAttribute ("background","/images/cbChecked.png");
}
else {
  inputValue.setAttribute ("value", "off")
  element.setAttribute ("background","/images/cbUnchecked.png");
}

}

  <td background="<%=

answer.iscorrect==‘Y’?’/images/cbChecked.png’:’/images/cbUnchecked.png’
%>" onClick=“checkboxClick(this)”>

Wow that is interesting. Thanks?!?

But I would still like to know how to use check_box.

Thanks again.

This is my first post here and I love the responsiveness. thanks

Thom

My personal experience is use it for waste paper.

This is also what I have seen in serious commercial web apps that I have
dissected.

David J. wrote:

Thanks again.

This is my first post here and I love the responsiveness. thanks

Thom

Take a look at this…

http://railsmanual.com/module/ActionView%3A%3AHelpers%3A%3AFormHelper/check_box

You probably are just not setting up the instance variables correctly.
It really does work well, but it’s kind of hard to debug with so little
of your code.

FYI, the rails check-box helper has a work around for submitting when
it’s not checked.

_Kevin

The business problem being solved was creating and editing a multiple
choice
question.

I was looking for iscorrect[]={‘N’,‘Y’,‘N’} to parallel the array of
answerText[] = {‘bla bla bla’,‘mam mam mam’,‘ya ya ya’}.

This well-published but clumsy workaround produced
iscorrect[]={‘Y’,‘Y’,‘Y’,‘Y’}, which is neither correct nor decipherable
into
a correct answer.

The error is not with Rails, but with the assumptions of the HTML
specification writers surrounding the usage of checkboxes (if it isn’t
checked you don’t want to know about it). It is a well known issue with
a
well published alternative pattern that works in all cases, regardless
of the
language platform or application framework of choice.

Th alternative iscorrect[]={‘N’,‘Y’,‘N’,‘N’} requires enough extra work
to
process that it is still not “better” than the pattern I finally used
that
simply returns what I need in the first place.

_Kevin wrote:

David J. wrote:

Thanks again.

This is my first post here and I love the responsiveness. thanks

Thom

Take a look at this…

http://railsmanual.com/module/ActionView%3A%3AHelpers%3A%3AFormHelper/check_box

You probably are just not setting up the instance variables correctly.
It really does work well, but it’s kind of hard to debug with so little
of your code.

FYI, the rails check-box helper has a work around for submitting when
it’s not checked.

_Kevin

What additional code do I need to put up here? I basically created a
database and then the generic model, controller, view. Nothing fancy at
all. I have written no code to speak of. Maybe I should have put NOOB in
all caps because I feel pretty stupid at this point.

On Friday 22 September 2006 07:08, Thom M. wrote:

Maybe I should have put NOOB in
all caps because I feel pretty stupid at this point.

I have about 30 years programming experience, and I chased my tail for a
week
on this one. The ones who should feel stupid are the people that
figured
that a checkbox’s contents should only be transmitted if it is set, when
they
put together the specification for the widget in the HTML specification.

That’s why I’m partial to a single code idiom that is guaranteed to work
for
all situations. Unfortunately, now I have to work out what to do for
text
browsers like lynx :o(. Fortunately, they are not a big player for my
curent target audience.

Thom,
What works for me is instead of the database field being set to 1
(tinyint) It is set to true (boolean). I have confirmed that this
does not work in oracle through, as my friend and I had the same
problem, his database was mysql and mine was oracle. (his works).
Something about my oracle that doesn’t do true, only 1s… HTH

Brian H. wrote:

@Thom:

This doesn’t work for you?

You had

<% for @prod_detail in @product.prod_detail %>

Color:<%= prod_detail.color %> Size:<%= prod_detail.size %> Price:<%= prod_detail.price %> Available: <%= check_box ("prod_detail","available") %>

<% end %>

I changed to
You had

<% for @prod_detail in @product.prod_detail %>

Color:<%= @prod_detail.color %> Size:<%= @prod_detail.size %> Price:<%= @prod_detail.price %> Available: <%= check_box ("prod_detail","available") %>

I had to at the @ in front of all of the prod_details and it worked.
Thanks so much…

I wish I understood why that worked but I am stoked now.

@Thom:

This doesn’t work for you?

<% for @prod_detail in @product.prod_detail %>

Color:<%= prod_detail.color %> Size:<%= prod_detail.size %> Price:<%= prod_detail.price %> Available: <%= check_box ("prod_detail","available") %>

<% end %>

Notice some changes to your code

You had

<% for prod_detail in @product.prod_detail %>

I have

<% for @prod_detail in @product.prod_detail %>

Secondly, you had

<%= check_box (“available”, prod_detail.available) %>

I had

<%= check_box (“prod_detail”, “available”) %>

Notice that the check_box takes two params + options… OBJECT,
METHOD. If
you want “prod_detail.available” you use it just like a text field

I have similar code in an app I’m using right now and it works fine…
no
need for strange workarounds as the check_box helper already adds a
hidden
field to handle those situations where the value is uncheked.

The helpers like text_field, etc expect an instance variable (defined
with
@) to do some of their magic. You’ll run into this a lot.

No one want to take a crack at why it is doing this???

The issues that have been explored so far in this thread are:

  1. The HTML specification for checkboxes is not ideal, so many serious
    web
    apps write their own checkboxes. The standard idiom for a graphical
    checkbox
    widget was supplied.

  2. @prod_detail and prod_detail are two distinctly different variables.
    the
    former is an instance scoped variable, and the other is method scoped.
    The @
    is actually meaningful in the variable’s name.

  3. MySQL supports a non-standard boolean type, which SQL-92 compliant
    RDBMS’
    gnererally do not support. The standard idioms for representing
    booleans in
    SQL-92 compliant databases are char(1) constrained to ‘Y’/‘N’, ‘T’/‘F’,
    or ‘1’/‘0’, or numeric(1) constratined to 1/0.

I don’t really know what else there is to cover. At this point, it
might be
worthwhile to put “p @prod_detail” and/or “p prod_detail” on every other
line
and watch the contents of @prod_detail in your terminal window.

Brian H. wrote:

The helpers like text_field, etc expect an instance variable (defined
with
@) to do some of their magic. You’ll run into this a lot.

Ok… I officially hate checkboxes… Here is the next problem

This section works great
<% for @prod_detail in @product.prod_details %>

Color:<%= @prod_detail.color %> Size:<%= @prod_detail.size %> Price:<%= @prod_detail.price %> Available: <%= check_box ("prod_detail","available") %>


<% end %>

When I added this to the bottom the text field is already filled out
with what is in the data that is in the last record of the table. I.E.
if the last record is blue for color then the text field has a value of
blue. If I remove the @ from the prod_detail after the for and the
color,size,price it works fine but then the check box stops functioning
again. AHHHHHHHHHH
<%= form_tag :action => “prod_add”, :id => @product%>
<%= text_field “prod_detail”, “color”%>
<%= submit_tag “Add Option”%>

Thanks in advance AGAIN…
Thom