# Mystified by forms and models

**URL:** <https://www.ruby-forum.com/t/mystified-by-forms-and-models/106083>\
**Category:** Rails\
**Created:** [June 6, 2007, 7:42am UTC](https://www.ruby-forum.com/t/mystified-by-forms-and-models/106083 "2007-06-06T07:42:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 6, 2007, 7:42am UTC](https://www.ruby-forum.com/t/mystified-by-forms-and-models/106083/1 "2007-06-06T07:42:22Z")

</div>

Okay. Either I’m missing something WAY basic or Rails and its API are  
lying to me (experience tells me it’s the former). Here is the basic  
setup for a check\_box tag in the API:

\<%= check\_box(‘object’, ‘method’) %\>

Right?  
So here’s what I type in.

\<%= check\_box(‘todoitems’, ‘done’) %\>

“todoitems” is my table, and “done” is one column in that table. Hence,  
it is todoitem’s method. But Rails keeps giving me this error:

undefined method `done’ for #Array:0x416fb18

Now, I think that this has something to do with me making these  
checkboxes in a for…in loop. But I don’t understand what I’m doing  
wrong. Anybody care to enlighten me?

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 6, 2007, 7:56am UTC](https://www.ruby-forum.com/t/mystified-by-forms-and-models/106083/2 "2007-06-06T07:56:09Z")

</div>

Sean C. wrote:

> “todoitems” is my table, and “done” is one column in that table. Hence,  
> it is todoitem’s method. But Rails keeps giving me this error:
> 
> undefined method `done’ for #Array:0x416fb18
> 
> Now, I think that this has something to do with me making these  
> checkboxes in a for…in loop. But I don’t understand what I’m doing  
> wrong. Anybody care to enlighten me?

You’ve set @todoitems to either a has\_many association or the result  
of a find(:all,…,) – a collection or an array of ActiveRecord model  
objects. However Rails’ AR form helpers expect their first argument to  
be  
the name of an instance variable that holds a single ActiveRecord  
object.

So you want

```
<% for @todoitem in @todoitems %>
  <%= check_box :todoitem, :done %>
<% end %>

```

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 6, 2007, 9:01am UTC](https://www.ruby-forum.com/t/mystified-by-forms-and-models/106083/3 "2007-06-06T09:01:57Z")

</div>

Great. Thanks, Mark. That stops it from breaking.

But I’m left with the bigger problem now. Writing it as \<%= check\_box  
:todoitem, :done %\> means that every check box comes out in HTML the  
same. Like this:

Each one of these checkboxes has to be unique so that the function I’m  
going to write for them to change them into checked checkboxes knows  
which record to modify, right? Like this one that I found in a tutorial  
on the web:

\<%= check\_box :todoitem, :done, “onclick” =\>  
“document.location.href=’/todoitems/toggle\_check/#{t.id}’” %\>

The guy is basically hacking together a URL and strapping it to an  
onclick event. But this way only lets you send one value with it: the id  
of the checkbox item. I need to send the id of the project that it’s  
associated with as well. Like this maybe:

\<%= check\_box :todoitem, :done, “onclick” =\>  
“document.location.href=’/todoitems/toggle\_check/#{t.id}&projectid=#{@thisproject.id}’”  
%\>

But Rails complains that it can’t find the value in params[:projectid]  
when I do this.

Is there a way to do this?

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 6, 2007, 11:31am UTC](https://www.ruby-forum.com/t/mystified-by-forms-and-models/106083/4 "2007-06-06T11:31:35Z")

</div>

API says - check\_box(object\_name, method, options = {}, checked\_value  
= “1”, unchecked\_value = “0”)

Also give the following example:  
check\_box(“puppy”, “gooddog”, {}, “yes”, “no”)

in light of that you can use the following:  
\<% for todoitem in @todoitems %\>  
\<%= check\_box :todoitem, todoitem.done, {}, todoitem.id, nil %\>  
\<% end %\>  
This should set the id as the checked value and nil to the unchecked  
vaue, but you can use whatever column you want.

Cam

On Jun 6, 5:01 pm, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 6, 2007, 12:36pm UTC](https://www.ruby-forum.com/t/mystified-by-forms-and-models/106083/5 "2007-06-06T12:36:48Z")

</div>

Sean C. wrote:

> going to write for them to change them into checked checkboxes knows  
> which record to modify, right? Like this one that I found in a tutorial  
> on the web:

If the todoitems already exist in the database, you can have their  
ids sent in a normal form post by using

```
<%= check_box 'todoitem[]', :done %>

```

The project\_id should either be a part of the form url, or set  
in a form hidden field.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![smc7000](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ed655f/32.png) [@smc7000](https://www.ruby-forum.com/u/smc7000)\
**Post date:** [June 7, 2007, 3:37am UTC](https://www.ruby-forum.com/t/mystified-by-forms-and-models/106083/6 "2007-06-07T03:37:03Z")

</div>

Thanks guys. Working now!
