A todo list

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

heres the template

My Todo List

My Todo List

<% @tasklists.each do |tasklist| %>
<%= check_box(“task”, “done”) %>
<%= tasklist.task %>
<%= link_to(“Edit”, :action => “edit”, :id => @tasklists.id) %>


<%end%>

--------------------------------

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

i get the task i want to edit

can anyone help me?!

thank

steve

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

thank

steve


Posted via http://www.ruby-forum.com/.


“Remember. Understand. Believe. Yield! → http://ruby-lang.org

Jeff W.

thanx that was the error

im starting to love ruby :slight_smile:

ciao

steve

stevanicus wrote:

<% @tasklists.each do |tasklist| %>
<%= check_box(“task”, “done”) %>
<%= tasklist.task %>
<%= link_to(“Edit”, :action => “edit”, :id => @tasklists.id) %>


<%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).

hmm no i cant get something else to work

in my controller

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

in my template

New Task: <%= text_field("new_tasklist", "task") %>

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

and i get the following error

SyntaxError in #

./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

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
from my previous posting ) has/have absolutely NOTHING to do with ruby’s
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.

On 12/5/05, Mike F. [email protected] wrote:

What you’ve done (just to expand on why it doesn’t work) is you’ve


“Remember. Understand. Believe. Yield! → http://ruby-lang.org

Jeff W.

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).

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 %>


<%= link_to ‘Test on Array instance’, :action => ‘show’, :id =>
Array.new() %>


<%= link_to ‘Test on Array w/id’, :action => ‘show’, :id =>
Array.new().id %>

You’ll see the difference in the anchors produced:

Test on AR instance


Test on Array instance


Test on Array w/id

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

steven masala wrote:

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.

On 12/5/05, gregarican [email protected] wrote:

    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.

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:

class MyController
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

def other_action
  # body of other action
end

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 F.

Jacob F. 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.