Forum: Ruby on Rails Mystified by forms and models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sean C. (Guest)
on 2007-06-06 09:42
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?
Mark Reginald J. (Guest)
on 2007-06-06 09:56
(Received via mailing list)
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.
Sean C. (Guest)
on 2007-06-06 11:01
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:

<input id="todoitem_done" name="todoitem[done]" type="checkbox"
value="1" /><input name="todoitem[done]" type="hidden" value="0" />

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?
cammo (Guest)
on 2007-06-06 13:31
(Received via mailing list)
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")
    <input type="checkbox" id="puppy_gooddog" name="puppy[gooddog]"
value="yes" />
    <input name="puppy[gooddog]" type="hidden" value="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>
Mark Reginald J. (Guest)
on 2007-06-06 14:36
(Received via mailing list)
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.
Sean C. (Guest)
on 2007-06-07 05:37
Thanks guys. Working now!
This topic is locked and can not be replied to.