check_box for Noob

Hi!

Try this:

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

1 != true in ruby.

Markus

Markus Piff wrote:

Hi!

Try this:

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

1 != true in ruby.

Markus

>
>
> 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 %>
> <p>
> <b>Color:</b><%= prod_detail.color %></br>
> <b>Size:</b><%= prod_detail.size %></br>
> <b>Price:</b><%= prod_detail.price %></br>
> <b>Available:</b>
>
> --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
> %> />
>
> </p>
> <% end %>
>
> So why does one work and the other does not?
> Thanks in advance
> Thom
>
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>

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

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)"><input type="hidden"
name="iscorrect[]" value="<%= answer.iscorrect=='Y'?'on':'off' %>"
style="background-repeat: no-repeat"/></td>

Thom Morrow wrote:

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 Johnson wrote:

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.

> 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

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.

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