Check_box_tag always checked

#check_box and #check_box_tag are going to be the end of me. Why is
this always checked?

<% checked = @reservation.selected ? ‘checked’ : nil %>

<%= check_box_tag("reservation[" + @reservation.id.to_s + "][selected]", 'checked', :checked => checked) %>

Well, first, you don’t need the :checked => checked. Whether it is
checked or not is determined by the third argument being true or false.

This will be named “check_box”, will have id “check_box”, a value of 1,
and will be checked:
check_box_tag(:checked_box, 1, true)

This is the same as above, but won’t be checked
check_box_tag(:checked_box, 1, false)

Does this help?

Yeah I was confused because I had looked at the source for the method,
and I
knew what you just said, but checked was evaluating improperly. Which
made
me think I misunderstood the method source. So I went looking around
and
found examples talking about using “:checked => …” and screwed me up
even
further.

Thanks

I figured it out

<% checked = (@reservation.selected == ‘checked’) ? ‘checked’ : nil %>

<%= check_box_tag("reservation[" + @reservation.id.to_s + "][selected]", 'checked', checked) %>

@reservation.selected being 0 or ‘checked’ still returns true, which
sets
checked to ‘checked’ no matter what

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs