sortable_element : You have a nil object when you didn't expect it!

13 views
Skip to first unread message

Mr_Noob

unread,
Dec 13, 2007, 5:16:58 AM12/13/07
to PeepCode
Hi all,

Situation :

# ruby -v
ruby 1.8.5 (2006-08-25) [i486-linux]

# rails -v
Rails 2.0.1

I am trying to create the same sortable list that is explained on
"rail from scratch part 1" screencast.
So i write app/views/animals/index.rhtml this way :

<ul id="animal_list">
<li>Dog</li>
<li>Pig</li>
<li>Robot</li>
</ul>

<%= sortable_element "animal_list" -%>

Then i create app/views/layouts/application.rhtml :

<html>
<head>
<%= javascript_include_tag :defaults -%>
</head>
<body>
<%= yield -%>
</body>
</html>

After launching script/server, instead of obtaining the same nice
sortable list as in the screencast, i have this following error on my
browser :

NoMethodError in Animals#index

Showing animals/index.rhtml where line #7 raised:

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]

Extracted source (around line #7):

4: <li>Robot</li>
5: </ul>
6:
7: <%= sortable_element "animal_list" -%>

any idea?

thank you in advance

topfunky

unread,
Dec 13, 2007, 12:03:20 PM12/13/07
to PeepCode
On Dec 13, 2:16 am, Mr_Noob <gniag...@gmail.com> wrote:
> I am trying to create the same sortable list that is explained on
> "rail from scratch part 1" screencast.
> So i write app/views/animals/index.rhtml this way :
>
> <%= sortable_element "animal_list" -%>

Rails 2.0 has made the :url argument mandatory for the
sortable_element method. Try this and you'll be in business:

<%= sortable_element "animal_list", :url => { :action => "hello" } -
%>

In the example, the url is irrelevant, but Rails 2.0 wants one anyway.

Yesterday, I uploaded new copies of Rails from Scratch Part I & II
with notes for Rails 2.0 compatibility and updated code samples
(including this problem). You can revisit your account and download a
new copy of the Full download to get the Rails 2.0 notes and code for
these screencasts.

The screencast itself will be updated soon to mention these issues.

Geoffrey Grosenbach
http://peepcode.com

Scott

unread,
Dec 16, 2007, 2:55:04 PM12/16/07
to PeepCode
Geoffrey, do you know why this requirement was added? Also, you say
that the url is irrelevant but sortable_element still makes an
unnecessary ajax call to the server which adds overhead to the client
and the server. It doesn't make sense to me why this requirement was
added.

Scott

topfunky

unread,
Dec 18, 2007, 1:35:24 PM12/18/07
to PeepCode
On Dec 16, 11:55 am, Scott <scott...@gmail.com> wrote:
> Geoffrey, do you know why this requirement was added?

I'm not sure why it was added. The assumption may be that the default
use of the sortable tag is to sort something and then notify the
server about it.

It was used as an introductory example in the screencast, so the hit
to the server wasn't needed. You can also use Scriptaculous sortables
directly (in Javascript) without hitting a server.

> Also, you say
> that the url is irrelevant but sortable_element still makes an
> unnecessary ajax call to the server which adds overhead to the client
> and the server. It doesn't make sense to me why this requirement was
> added.

I think that in most cases, you would want to hit the server with the
results of the sort. However, this requires more controller and model
code than was being taught at this point in the screencast.

As for the performance hit, it's pretty minimal. If you really wanted
to do a sort and not hit the server, you could use Javascript directly
as mentioned above.

Geoffrey Grosenbach
http://peepcode.com




Reply all
Reply to author
Forward
0 new messages