Mystified by forms and models

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?

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.

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?

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. [email protected]

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.

Thanks guys. Working now!