# You have a nil object when you didn't expect it?

**URL:** <https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/146406>\
**Category:** Rails\
**Created:** [August 20, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/146406 "2008-08-20T18:13:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2008, 6:13pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/146406/1 "2008-08-20T18:13:46Z")

</div>

Hello,

I’m trying a simple AJAX test link with the following code inside my  
view :

\<%= link\_to\_remote( “[Total]”, { :action =\> “total”, :update =\>  
“letotal” } ) %\>

In my controller I put :

def total  
@res = Person.find(:all)  
render\_text “Il y a #{@res.size} personnes dans le carnet”  
end

When I test it, I get the following error message :

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.[]

Thank you for your help

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2008, 6:18pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/146406/2 "2008-08-20T18:18:27Z")

</div>

On 20 Aug 2008, at 17:13, removed\_email\_address@domain.invalid wrote:

> In my controller I put :
> 
> def total  
> @res = Person.find(:all)  
> render\_text “Il y a #{@res.size} personnes dans le carnet”  
> end

Unrelated to this but it’s way way more efficient to say  
@number\_of\_people = Person.count rather that use Person.find(:all).size

> When I test it, I get the following error message :
> 
> 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.[]

What’s the line throwing that exception ?

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2008, 6:20pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/146406/3 "2008-08-20T18:20:12Z")

</div>

\*on \<%= link\_to\_remote( “[Total]”, { :action =\> “total”, :update =\>

> “letotal” } ) %\>\*

If I put it in comment there’s no error.

On Wed, Aug 20, 2008 at 6:17 PM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2008, 6:22pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/146406/4 "2008-08-20T18:22:34Z")

</div>

c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/url\_rewriter.rb:102:in  
`rewrite_url' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_controller/url_rewriter.rb:88:in`rewrite’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_controller/base.rb:621:in  
`url_for' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_view/helpers/url_helper.rb:76:in`send’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_view/helpers/url\_helper.rb:76:in  
`url_for' c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action_view/helpers/prototype_helper.rb:461:in`remote\_function’  
c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.0/lib/action\_view/helpers/prototype\_helper.rb:255:in  
`link_to_remote' app/views/carnet/voir.html.erb:15:in`\_run\_erb\_47app47views47carnet47voir46html46erb’

On Wed, Aug 20, 2008 at 6:17 PM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 20, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/146406/5 "2008-08-20T18:21:23Z")

</div>

On 20 Aug 2008, at 17:19, Alexandre Brillant wrote:

> on \<%= link\_to\_remote( “[Total]”, { :action =\> “total”, :update =\>
> 
> > “letotal” } ) %\>
> 
> If I put it in comment there’s no error.

D’oh. That should be link\_to\_remote “[Total”], :url =\> {:action =\>  
‘total’}, :update =\> ‘letotal’

Fred

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 21, 2008, 8:52am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it/146406/6 "2008-08-21T08:52:27Z")

</div>

Thank you Fred

On Wed, Aug 20, 2008 at 6:20 PM, Frederick C. \<
