# A todo list

**URL:** <https://rubytalk.org/t/a-todo-list/23019>\
**Category:** ruby-talk\
**Created:** [5 December 2005 17:29 UTC](https://rubytalk.org/t/a-todo-list/23019 "2005-12-05T17:29:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven\_masala](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@steven\_masala](https://rubytalk.org/u/steven_masala)\
**Post date:** [5 December 2005 17:29 UTC](https://rubytalk.org/t/a-todo-list/23019/1 "2005-12-05T17:29:03Z")

</div>

did a tutorial on ruby on rails site, however the edit isnt working

heres the template

> **···**
>
> -------------------------------  
> \<html\>  
> &nbsp;&nbsp;\<head\>  
> &nbsp;&nbsp;&nbsp;\<title\>My Todo List\</title\>  
> &nbsp;&nbsp;\</head\>
> 
> \<body\>  
> \<h1\>My Todo List\</h1\>
> 
> \<% @tasklists.each do |tasklist| %\>  
> \<%= check\_box("task", "done") %\>  
> \<%= tasklist.task %\>  
> \<%= link\_to("Edit", :action =\> "edit", :id =\> @tasklists.id) %\>  
> \<br /\>  
> \<%end%\>
> 
> \</body\>  
> \</html\>  
> --------------------------------
> 
> when i press edit it gives me
> 
> ActiveRecord::RecordNotFound in Tasklist#edit
> 
> Couldn't find Tasklist with ID=29134536
> 
> but if i type in the address box
> 
> [http://127.0.0.1:3000/tasklist/edit/1](http://127.0.0.1:3000/tasklist/edit/1)
> 
> i get the task i want to edit
> 
> can anyone help me?!
> 
> thank
> 
> steve
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeff\_Wood](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Jeff\_Wood](https://rubytalk.org/u/Jeff_Wood)\
**Post date:** [5 December 2005 17:47 UTC](https://rubytalk.org/t/a-todo-list/23019/2 "2005-12-05T17:47:53Z")

</div>

Actually, I believe:

\<%= link\_to("Edit", :action =\> "edit", :id =\> @tasklists.id) %\>

should be

\<%= link\_to("Edit", :action =\> "edit", :id =\> tasklist) %\>

... should be all you need.

j.

> **···**
>
> On 12/5/05, steven masala \<smasala@gmail.com\> wrote:
> 
> > did a tutorial on ruby on rails site, however the edit isnt working
> > 
> > heres the template  
> > -------------------------------  
> > \<html\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<head\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<title\>My Todo List\</title\>  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\</head\>
> > 
> > \<body\>  
> > \<h1\>My Todo List\</h1\>
> > 
> > \<% @tasklists.each do |tasklist| %\>  
> > \<%= check\_box("task", "done") %\>  
> > \<%= tasklist.task %\>  
> > \<%= link\_to("Edit", :action =\> "edit", :id =\> @tasklists.id) %\>  
> > \<br /\>  
> > \<%end%\>
> > 
> > \</body\>  
> > \</html\>  
> > --------------------------------
> > 
> > when i press edit it gives me
> > 
> > ActiveRecord::RecordNotFound in Tasklist#edit
> > 
> > Couldn't find Tasklist with ID=29134536
> > 
> > but if i type in the address box
> > 
> > [http://127.0.0.1:3000/tasklist/edit/1](http://127.0.0.1:3000/tasklist/edit/1)
> > 
> > i get the task i want to edit
> > 
> > can anyone help me?!
> > 
> > thank
> > 
> > steve
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> "Remember. Understand. Believe. Yield! -\> [http://ruby-lang.org](http://ruby-lang.org)"
> 
> Jeff Wood

---

<div class="post-metadata">

**Author:** ![Mike\_Fletcher](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mike\_Fletcher](https://rubytalk.org/u/Mike_Fletcher)\
**Post date:** [5 December 2005 18:23 UTC](https://rubytalk.org/t/a-todo-list/23019/3 "2005-12-05T18:23:50Z")

</div>

stevanicus wrote:

> \<% @tasklists.each do |tasklist| %\>  
> \<%= check\_box("task", "done") %\>  
> \<%= tasklist.task %\>  
> \<%= link\_to("Edit", :action =\> "edit", :id =\> @tasklists.id) %\>  
> \<br /\>  
> \<%end%\>

What you've done (just to expand on \*why\* it doesn't work) is you've  
called the "id" method on @tasklists which is an Array instance, not an  
Active::Record descended model object. So it's returning the unique  
Object#object\_id instead of a database record id (which is why your  
error message says "29134536" instead of "1"). In fact if you look at  
your logs you'll probably see a gripe that Object#id is deprecated and  
you should use Object#object\_id instead.

$ ruby -le 'puts Array.new().id'  
-e:1: warning: Object#id will be deprecated; use Object#object\_id  
941526

As you can see that number's not going to be anywhere near what your  
valid record ids are.

What you want to do is pass the AR instance to link\_to, or call the id  
method on that instead (which link\_to does behind the scenes if passed  
an object which implements "id"; your problem was that while Array can  
do id it's not the right id method).

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![steven\_masala](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@steven\_masala](https://rubytalk.org/u/steven_masala)\
**Post date:** [5 December 2005 18:18 UTC](https://rubytalk.org/t/a-todo-list/23019/4 "2005-12-05T18:18:00Z")

</div>

thanx that was the error

im starting to love ruby 🙂

ciao

steve

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jeff\_Wood](https://avatars.discourse-cdn.com/v4/letter/j/9fc29f/32.png) [@Jeff\_Wood](https://rubytalk.org/u/Jeff_Wood)\
**Post date:** [5 December 2005 18:38 UTC](https://rubytalk.org/t/a-todo-list/23019/5 "2005-12-05T18:38:13Z")

</div>

Actually, your information is a bit off-center. Although yes, the current  
versions of ruby do warn about use of object#id being deprecated, the #id  
method for ActiveRecord objects is actually a reference to the unique value  
for the given row of the database...

When used in link\_to statement within rails the :id =\> item syntax simply  
assigns the unique id of the record to a hidden form element under the name  
"id" .

So, I hope that helps provide context. The above code ( and/or the code  
object#id.

Hope that helps clear things up, because use of the two ( ActiveRecord#id  
vs. Object#id ) are quite 180 from each other.

j.

> **···**
>
> from my previous posting ) has/have absolutely NOTHING to do with ruby's
> 
> On 12/5/05, Mike Fletcher \<lemurific+rforum@gmail.com\> wrote:
> 
> > stevanicus wrote:  
> > \> \<% @tasklists.each do |tasklist| %\>  
> > \> \<%= check\_box("task", "done") %\>  
> > \> \<%= tasklist.task %\>  
> > \> \<%= link\_to("Edit", :action =\> "edit", :id =\> @tasklists.id) %\>  
> > \> \<br /\>  
> > \> \<%end%\>
> > 
> > What you've done (just to expand on \*why\* it doesn't work) is you've  
> > called the "id" method on @tasklists which is an Array instance, not an  
> > Active::Record descended model object. So it's returning the unique  
> > Object#object\_id instead of a database record id (which is why your  
> > error message says "29134536" instead of "1"). In fact if you look at  
> > your logs you'll probably see a gripe that Object#id is deprecated and  
> > you should use Object#object\_id instead.
> > 
> > $ ruby -le 'puts Array.new().id'  
> > -e:1: warning: Object#id will be deprecated; use Object#object\_id  
> > 941526
> > 
> > As you can see that number's not going to be anywhere near what your  
> > valid record ids are.
> > 
> > What you want to do is pass the AR instance to link\_to, or call the id  
> > method on that instead (which link\_to does behind the scenes if passed  
> > an object which implements "id"; your problem was that while Array can  
> > do id it's not the right id method).
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> "Remember. Understand. Believe. Yield! -\> [http://ruby-lang.org](http://ruby-lang.org)"
> 
> Jeff Wood

---

<div class="post-metadata">

**Author:** ![steven\_masala](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@steven\_masala](https://rubytalk.org/u/steven_masala)\
**Post date:** [5 December 2005 18:58 UTC](https://rubytalk.org/t/a-todo-list/23019/6 "2005-12-05T18:58:49Z")

</div>

hmm no i cant get something else to work

in my controller

&nbsp;&nbsp;def add\_tasklist  
&nbsp;&nbsp;&nbsp;&nbsp;tasklist = Tasklist.new  
&nbsp;&nbsp;&nbsp;&nbsp;tasklist.attributes = @params["new\_tasklist"]

&nbsp;&nbsp;&nbsp;&nbsp;if tasklist.save  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:action =\> "list")  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render\_text"could not add"  
&nbsp;&nbsp;end

in my template

\<form method="post" action="add\_tasklist"\>  
New Task:  
\<%= text\_field("new\_tasklist", "task") %\>  
\<input type="submit" value="Add Task"\>

my database is todo, the table is tasklist, fields are id, task, done

and i get the following error

SyntaxError in \<controller not set\>#\<action not set\>

./script/../config/../app/controllers/tasklist\_controller.rb:17: syntax  
error

ive tried changing the tasklist to tasklists, im not quite sure what  
that means why some are tasklists and some tasklist.

thanx

Steve

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Mike\_Fletcher](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mike\_Fletcher](https://rubytalk.org/u/Mike_Fletcher)\
**Post date:** [5 December 2005 19:16 UTC](https://rubytalk.org/t/a-todo-list/23019/7 "2005-12-05T19:16:54Z")

</div>

jeff.darklight wrote:

> When used in link\_to statement within rails the :id =\> item syntax  
> simply  
> assigns the unique id of the record to a hidden form element under the  
> name  
> "id" .

Right. And it's smart enough to not just blindly call id on what's  
passed (I thought would, but I just actually tried it and I'm wrong) to  
get that unique value. My mention of the deprecation warning was a red  
herring.

> So, I hope that helps provide context. The above code ( and/or the code  
> from my previous posting ) has/have absolutely NOTHING to do with ruby's  
> object#id.

However the original code:

> \<%= link\_to("Edit", :action =\> "edit", :id =\> @tasklists.id) %\>

\*is\* calling Object#id via an Array instance rather than letting AR do  
it's magic. That's why there's a value for id, and also why it's a  
useless value. Had he done ":id =\> @tasklists" instead there wouldn't  
have been an id value on the end (pointing at ".../tasklist/edit/"  
instead of ".../tasklist/edit/29134536" like it did) and he would have  
had a different problem (depending on how his edit action handles a  
missing id as opposed to an invalid one).

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Mike\_Fletcher](https://avatars.discourse-cdn.com/v4/letter/m/ba8739/32.png) [@Mike\_Fletcher](https://rubytalk.org/u/Mike_Fletcher)\
**Post date:** [5 December 2005 19:24 UTC](https://rubytalk.org/t/a-todo-list/23019/8 "2005-12-05T19:24:40Z")

</div>

fletch wrote:

> However the original code:
> 
> > \<%= link\_to("Edit", :action =\> "edit", :id =\> @tasklists.id) %\>
> 
> \*is\* calling Object#id via an Array instance rather than letting AR do  
> it's magic. That's why there's a value for id, and also why it's a  
> useless value. Had he done ":id =\> @tasklists" instead there wouldn't  
> have been an id value on the end (pointing at ".../tasklist/edit/"  
> instead of ".../tasklist/edit/29134536" like it did) and he would have  
> had a different problem (depending on how his edit action handles a  
> missing id as opposed to an invalid one).

If you try code like the original:

\<%= link\_to 'Test on AR instance', :action =\> 'show', :id =\> @foo %\>  
\<br /\>  
\<%= link\_to 'Test on Array instance', :action =\> 'show', :id =\>  
Array.new() %\>  
\<br /\>  
\<%= link\_to 'Test on Array w/id', :action =\> 'show', :id =\>  
Array.new().id %\>

You'll see the difference in the anchors produced:

\<a href="/rates/show/8"\>Test on AR instance\</a\>  
\<br /\>  
\<a href="/rates/show/"\>Test on Array instance\</a\>  
\<br /\>  
\<a href="/rates/show/542249508"\>Test on Array w/id\</a\>

And you do get the deprecation warning for the last one (from the output  
of script/server in this case):

./script/../config/../app/views/test/show.rhtml:6: warning: Object#id  
will be deprecated; use Object#object\_id

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![gregarican](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@gregarican](https://rubytalk.org/u/gregarican)\
**Post date:** [5 December 2005 21:22 UTC](https://rubytalk.org/t/a-todo-list/23019/9 "2005-12-05T21:22:33Z")

</div>

steven masala wrote:

> hmm no i cant get something else to work
> 
> in my controller
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def add\_tasklist  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasklist = Tasklist.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasklist.attributes = @params["new\_tasklist"]
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if tasklist.save  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:action =\> "list")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render\_text"could not add"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end
> 
> in my template
> 
> \<form method="post" action="add\_tasklist"\>  
> New Task:  
> \<%= text\_field("new\_tasklist", "task") %\>  
> \<input type="submit" value="Add Task"\>
> 
> my database is todo, the table is tasklist, fields are id, task, done
> 
> and i get the following error
> 
> SyntaxError in \<controller not set\>#\<action not set\>
> 
> /script/../config/../app/controllers/tasklist\_controller.rb:17: syntax  
> error
> 
> ive tried changing the tasklist to tasklists, im not quite sure what  
> that means why some are tasklists and some tasklist.
> 
> thanx
> 
> Steve

You closed your controller method with def...end but not the  
conditional if...else...end. Add an extra 'end' to the bottom of your  
code and you should be okay.

---

<div class="post-metadata">

**Author:** ![Jacob\_Fugal](https://avatars.discourse-cdn.com/v4/letter/j/439d5e/32.png) [@Jacob\_Fugal](https://rubytalk.org/u/Jacob_Fugal)\
**Post date:** [5 December 2005 21:49 UTC](https://rubytalk.org/t/a-todo-list/23019/10 "2005-12-05T21:49:29Z")

</div>

Well not necessarily at the end of the code. It should be added in  
place right after the else block (the way I indicated in my reply to  
your other post, Steven). To see why you can't just blindly put it at  
the end of your code, imagine his class actually looks like the  
following:

&nbsp;&nbsp;class MyController  
&nbsp;&nbsp;&nbsp;&nbsp;def add\_tasklist  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasklist = Tasklist.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasklist.attributes = @params["new\_tasklist"]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if tasklist.save  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to(:action =\> "list")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render\_text"could not add"  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def other\_action  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# body of other action  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

Ruby sees the first end after the else as belonging to the if/else.  
The definition of other\_action then ends up being part of the body of  
add\_tasklist, and the final end closes the definition of add\_tasklist.  
The class remains unclosed and that's what causes the syntax error. If  
you add an "end" at the end of the file, you close the class and  
remove the syntax error, but the code is still broken, because the  
definition of other\_action is still local to the body of add\_tasklist,  
obviously not the desired functionality.

Jacob Fugal

> **···**
>
> On 12/5/05, gregarican \<greg.kujawa@gmail.com\> wrote:
> 
> > steven masala wrote:  
> > \> def add\_tasklist  
> > \> tasklist = Tasklist.new  
> > \> tasklist.attributes = @params["new\_tasklist"]  
> > \>  
> > \>  
> > \> if tasklist.save  
> > \> redirect\_to(:action =\> "list")  
> > \> else  
> > \> render\_text"could not add"  
> > \> end
> > 
> > You closed your controller method with def...end but not the  
> > conditional if...else...end. Add an extra 'end' to the bottom of your  
> > code and you should be okay.

---

<div class="post-metadata">

**Author:** ![gregarican](https://avatars.discourse-cdn.com/v4/letter/g/7feea3/32.png) [@gregarican](https://rubytalk.org/u/gregarican)\
**Post date:** [6 December 2005 13:37 UTC](https://rubytalk.org/t/a-todo-list/23019/11 "2005-12-06T13:37:34Z")

</div>

Jacob Fugal wrote:

> Well not necessarily at the end of the code. It should be added in  
> place right after the else block (the way I indicated in my reply to  
> your other post, Steven).

Gotcha. Didn't mean to confuse anyone with the suggestion. In his  
particular case it happened to be at the end of his snippet. Good point.
