# Simple Check Box Question

**URL:** <https://rubytalk.org/t/simple-check-box-question/19040>\
**Category:** ruby-talk\
**Created:** [17 June 2005 16:50 UTC](https://rubytalk.org/t/simple-check-box-question/19040 "2005-06-17T16:50:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Koidin](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@Matt\_Koidin](https://rubytalk.org/u/Matt_Koidin)\
**Post date:** [17 June 2005 16:50 UTC](https://rubytalk.org/t/simple-check-box-question/19040/1 "2005-06-17T16:50:33Z")

</div>

I'm new to ruby and rails and I have a simple question that is driving  
me crazy. I have a group of check boxes and I want to be able to  
default check some of them based on entries in a database.

My display.rhtml looks like this:

\<% for category in @categories %\>  
\<dd\>\<%= check\_box "category", [category.id](http://category.id) %\>\<%= [category.name](http://category.name) %\>\</dd\>  
\<dt\>&nbsp;\</dt\>  
\<% end %\>

If I was writing in HTML I would add a CHECKED option to the checkbox  
tag to have it default to checked. How do I do that with ruby?

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![Derek\_Greentree](https://avatars.discourse-cdn.com/v4/letter/d/e480ec/32.png) [@Derek\_Greentree](https://rubytalk.org/u/Derek_Greentree)\
**Post date:** [17 June 2005 22:10 UTC](https://rubytalk.org/t/simple-check-box-question/19040/2 "2005-06-17T22:10:33Z")

</div>

It's the third parameter to the call you're making, a boolean. False is  
not checked, true is checked.

[http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html](http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html)

i.e., in your above example,  
\<%= check\_box "category", [category.id](http://category.id), category.my\_boolean\_attribute?  
%\>  
Would check the checkbox if category.my\_boolean\_attribute? returned  
true and would leave it unchecked otherwise.

---

<div class="post-metadata">

**Author:** ![Matt\_Koidin](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@Matt\_Koidin](https://rubytalk.org/u/Matt_Koidin)\
**Post date:** [21 June 2005 01:55 UTC](https://rubytalk.org/t/simple-check-box-question/19040/3 "2005-06-21T01:55:35Z")

</div>

I tried that and it doesn't work (again, I might be misinterpreting the  
syntax).

If I write:

\<%= check\_box "category", [category.id](http://category.id), true %\>

I get an error that says:

undefined method `stringify\_keys' for true:TrueClass

---

<div class="post-metadata">

**Author:** ![Matt\_Koidin](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@Matt\_Koidin](https://rubytalk.org/u/Matt_Koidin)\
**Post date:** [21 June 2005 03:35 UTC](https://rubytalk.org/t/simple-check-box-question/19040/4 "2005-06-21T03:35:35Z")

</div>

I solved the problem --

I switched to using \<input type=checkbox\> and mimic'd the the hidden  
field that the check\_box tag gives you.

---

<div class="post-metadata">

**Author:** ![Devin\_Mullins](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@Devin\_Mullins](https://rubytalk.org/u/Devin_Mullins)\
**Post date:** [21 June 2005 12:21 UTC](https://rubytalk.org/t/simple-check-box-question/19040/5 "2005-06-21T12:21:55Z")

</div>

Matt Koidin wrote:

> I solved the problem --
> 
> I switched to using \<input type=checkbox\> and mimic'd the the hidden  
> field that the check\_box tag gives you.

Mehh.... Evil... A look at the documentation says how to do it:

[http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html#M000344](http://api.rubyonrails.com/classes/ActionView/Helpers/FormHelper.html#M000344)

\*check\_box\*(object, method, options = {}, checked\_value = "1", unchecked\_value = "0")

Returns a checkbox tag tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). It's intended that method returns an integer and if that integer is above zero, then the checkbox is checked. Additional options on the input tag can be passed as a hash with options. The checked\_value defaults to 1 while the default unchecked\_value is set to 0 which is convenient for boolean values. Usually unchecked checkboxes don't post anything. We work around this problem by adding a hidden value with the same name as the checkbox.

So your category\_id seems to be in the wrong place.

Devin

---

<div class="post-metadata">

**Author:** ![Jeff\_Waltzer1](https://avatars.discourse-cdn.com/v4/letter/j/dbc845/32.png) [@Jeff\_Waltzer1](https://rubytalk.org/u/Jeff_Waltzer1)\
**Post date:** [21 June 2005 18:48 UTC](https://rubytalk.org/t/simple-check-box-question/19040/6 "2005-06-21T18:48:33Z")

</div>

My apologies if this is a excessively newbish question.  
Is there way to get one's rails app loaded into fxri such that I can use it  
with Rails to explore what I can do with the different objects of my Rails  
application?

---

<div class="post-metadata">

**Author:** ![Jamis\_Buck2](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jamis\_Buck2](https://rubytalk.org/u/Jamis_Buck2)\
**Post date:** [21 June 2005 19:17 UTC](https://rubytalk.org/t/simple-check-box-question/19040/7 "2005-06-21T19:17:01Z")

</div>

Not having used fxri before, it should just be a matter of requiring your config/environment.rb file. Once you do that, you should have all of your Rails app before you, ready to query.

- Jamis

> **···**
>
> On Jun 21, 2005, at 12:48 PM, Jeff Waltzer wrote:
> 
> > My apologies if this is a excessively newbish question.  
> > Is there way to get one's rails app loaded into fxri such that I can use it  
> > with Rails to explore what I can do with the different objects of my Rails  
> > application?

---

<div class="post-metadata">

**Author:** ![Lothar\_Scholz2](https://avatars.discourse-cdn.com/v4/letter/l/f14d63/32.png) [@Lothar\_Scholz2](https://rubytalk.org/u/Lothar_Scholz2)\
**Post date:** [22 June 2005 04:15 UTC](https://rubytalk.org/t/simple-check-box-question/19040/8 "2005-06-22T04:15:12Z")

</div>

Hello Jeff,

> My apologies if this is a excessively newbish question.  
> Is there way to get one's rails app loaded into fxri such that I can use it  
> with Rails to explore what I can do with the different objects of my Rails  
> application?

Yes you can use "rdoc --ri \<your-rails-app-dir\>" and it will generate  
the data for the database. But make sure that you don't extend  
existing classes like STRING as they are removed.

I work on an easy way to do so with "Arachno Ruby" as the current  
ri/rdoc implementation is not the best.

> **···**
>
> --  
> Best regards, emailto: scholz at scriptolutions dot com  
> Lothar Scholz [http://www.ruby-ide.com](http://www.ruby-ide.com)  
> CTO Scriptolutions Ruby, PHP, Python IDE 's

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [22 June 2005 04:19 UTC](https://rubytalk.org/t/simple-check-box-question/19040/9 "2005-06-22T04:19:29Z")

</div>

Jeff Waltzer wrote:

> My apologies if this is a excessively newbish question.

[snip]

I can't answer your question -- hopefully others already have --  
but I will comment that newbie questions are welcome here.

Hal
