Forum: Ruby a todo list

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.
A29c862955ebed5e3f80308d2fab7360?d=identicon&s=25 Steven Masala (stevanicus)
on 2005-12-05 18:28
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
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 jeff.darklight (Guest)
on 2005-12-05 18:51
(Received via mailing list)
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:
> <body>
>
>
> thank
>
> steve
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


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

Jeff Wood
A29c862955ebed5e3f80308d2fab7360?d=identicon&s=25 Steven Masala (stevanicus)
on 2005-12-05 19:17
thanx that was the error

im starting to love ruby :)

ciao

steve
357558a6682f4d6624594763d9acdb35?d=identicon&s=25 Mike Fletcher (fletch)
on 2005-12-05 19:23
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).
A7c9c275318af9e1e3812fab9660cd7c?d=identicon&s=25 jeff.darklight (Guest)
on 2005-12-05 19:40
(Received via mailing list)
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 Fletcher <lemurific+rforum@gmail.com> 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 Wood
A29c862955ebed5e3f80308d2fab7360?d=identicon&s=25 Steven Masala (stevanicus)
on 2005-12-05 19:58
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
357558a6682f4d6624594763d9acdb35?d=identicon&s=25 Mike Fletcher (fletch)
on 2005-12-05 20:16
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).
357558a6682f4d6624594763d9acdb35?d=identicon&s=25 Mike Fletcher (fletch)
on 2005-12-05 20:24
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
33ec7e55a251c1be8d6febfd929aebbe?d=identicon&s=25 Greg Kujawa (gregarican)
on 2005-12-05 22:23
(Received via mailing list)
steven masala wrote:

>
> <form method="post" action="add_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.
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 lukfugl (Guest)
on 2005-12-05 22:52
(Received via mailing list)
On 12/5/05, gregarican <greg.kujawa@gmail.com> 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 Fugal
33ec7e55a251c1be8d6febfd929aebbe?d=identicon&s=25 Greg Kujawa (gregarican)
on 2005-12-06 14:41
(Received via mailing list)
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.
This topic is locked and can not be replied to.