# Evaluating nil in an autocomplete? :o(

**URL:** <https://www.ruby-forum.com/t/evaluating-nil-in-an-autocomplete-o/127519>\
**Category:** Rails\
**Created:** [January 30, 2008, 12:27am UTC](https://www.ruby-forum.com/t/evaluating-nil-in-an-autocomplete-o/127519 "2008-01-30T00:27:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![revanche](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@revanche](https://www.ruby-forum.com/u/revanche)\
**Post date:** [January 30, 2008, 12:27am UTC](https://www.ruby-forum.com/t/evaluating-nil-in-an-autocomplete-o/127519/1 "2008-01-30T00:27:33Z")

</div>

Can anyone help with the following? I’ve been trying to follow along  
with the tutorial for making a real-time search box with Ajax  
([http://wiki.rubyonrails.org/rails/pages/How+to+make+a+real-time+search+box+with+the+Ajax+helpers](http://wiki.rubyonrails.org/rails/pages/How+to+make+a+real-time+search+box+with+the+Ajax+helpers)),  
but have been hitting some snags along the way:

In the index view:  
  
\<%= observe\_field ‘search’, :frequency =\> 0.5,  
:update =\> ‘searchresults’, :url =\>  
{ :controller =\> ‘contacts’, :action=\> ‘list’ },  
:with =\> “‘search=’ + escape(value)” %\>

In the contacts controller

def list  
if @params[‘search’]  
@contacts\_pages, @contacts = paginate :contacts,  
:order\_by =\> ‘firstname’,  
:conditions =\> [ ‘LOWER(firstname) LIKE ?’,  
‘%’ + @params[‘search’].downcase + ‘%’ ],  
:per\_page =\> 20  
@mark\_term = @params[‘search’]  
render\_without\_layout  
else  
@contacts\_pages, @contacts = paginate :contacts,  
:order\_by =\> ‘firstname’, :per\_page =\> 20  
end  
end

And then finally in the list view.

\<%= @mark\_term ? highlight(contact.firstname, @mark\_term) :  
h(contact.firstname) %\>

Everytime I run it, the searchresults layer updates to show a error that  
isn’t that helpful:

NoMethodError in ContactsController#list

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

---

<div class="post-metadata">

**Author:** ![revanche](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ac91a4/32.png) [@revanche](https://www.ruby-forum.com/u/revanche)\
**Post date:** [January 30, 2008, 8:39pm UTC](https://www.ruby-forum.com/t/evaluating-nil-in-an-autocomplete-o/127519/2 "2008-01-30T20:39:49Z")

</div>

Are you working in Rails 2.x? The old default pagination helper (used  
in the “paginate :contacts” call) has been deprecated. That might  
explain the NoMethodError. If this is the case you might just try to  
get it working without pagination by calling Contact.find with the  
appropriate parms.

On Jan 29, 6:27 pm, Neil H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
