# Check\_box\_tag always checked

**URL:** <https://www.ruby-forum.com/t/check-box-tag-always-checked/114771>\
**Category:** Rails\
**Created:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/check-box-tag-always-checked/114771 "2007-09-17T13:25:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [September 17, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/check-box-tag-always-checked/114771/1 "2007-09-17T13:25:35Z")

</div>

#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) %\>

---

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [September 25, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/check-box-tag-always-checked/114771/2 "2007-09-25T23:04:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [September 25, 2007, 11:05pm UTC](https://www.ruby-forum.com/t/check-box-tag-always-checked/114771/3 "2007-09-25T23:05:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![creativelogic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@creativelogic](https://www.ruby-forum.com/u/creativelogic)\
**Post date:** [September 25, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/check-box-tag-always-checked/114771/4 "2007-09-25T23:09:57Z")

</div>

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
