a todo list

S

steven masala

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

heres the template
-------------------------------
<html>
<head>
<title>My Todo List</title>
</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


i get the task i want to edit


can anyone help me?!

thank

steve
 
J

Jeff Wood

------=_Part_34038_27441824.1133804866232
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Actually, I believe:

<%=3D link_to("Edit", :action =3D> "edit", :id =3D> @tasklists.id) %>

should be

<%=3D link_to("Edit", :action =3D> "edit", :id =3D> tasklist) %>

... should be all you need.

j.

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

heres the template
-------------------------------
<html>
<head>
<title>My Todo List</title>
</head>

<body>
<h1>My Todo List</h1>

<% @tasklists.each do |tasklist| %>
<%=3D check_box("task", "done") %>
<%=3D tasklist.task %>
<%=3D link_to("Edit", :action =3D> "edit", :id =3D> @tasklists.id) %>
<br />
<%end%>



</body>
</html>
--------------------------------

when i press edit it gives me

ActiveRecord::RecordNotFound in Tasklist#edit

Couldn't find Tasklist with ID=3D29134536


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


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood

------=_Part_34038_27441824.1133804866232--
 
M

Mike Fletcher

stevanicus said:
<% @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).
 
J

Jeff Wood

------=_Part_34769_28029101.1133807885223
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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 =3D> 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.

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


--
"Remember. Understand. Believe. Yield! -> http://ruby-lang.org"

Jeff Wood

------=_Part_34769_28029101.1133807885223--
 
S

steven masala

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

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

Mike Fletcher

jeff.darklight said:
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).
 
M

Mike Fletcher

fletch said:
However the original code:


*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
 
G

gregarican

steven said:
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


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

Jacob Fugal

steven said:
def add_tasklist
tasklist =3D Tasklist.new
tasklist.attributes =3D @params["new_tasklist"]


if tasklist.save
redirect_to:)action =3D> "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.

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 =3D Tasklist.new
tasklist.attributes =3D @params["new_tasklist"]
if tasklist.save
redirect_to:)action =3D> "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 Fugal
 
G

gregarican

Jacob said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top