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

**URL:** <https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it-error/183715>\
**Category:** Rails\
**Created:** [February 19, 2010, 12:20am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it-error/183715 "2010-02-19T00:20:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@sam](https://www.ruby-forum.com/u/sam)\
**Post date:** [February 19, 2010, 12:20am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it-error/183715/1 "2010-02-19T00:20:16Z")

</div>

Hi, I’ve created a business form and an address form using scaffolding  
and I am trying to get the business form to display some information  
from the address form, but I keep getting an “You have a nil object  
when you didn’t expect it! You might have expected an instance of  
Array. The error occurred while evaluating nil.each” error.

17:   
18:  
19: \<% for business in @businesses %\>  
20: \<% for address in @addresses %\>  
21:  
22:

  
23: \<%=h business.address\_id %\>

This is my business index.html form:

# Listing businesses

\<% for business in @businesses %\>  
\<% for address in @addresses %\>

\<% end %\> \<% end %\>

| Address | Business name | Business email | Business telephone | City |
| --- | --- | --- | --- | --- |
| \<%=h business.address\_id %\> | \<%=h business.business\_name %\> | \<%=h business.business\_email %\> | \<%=h business.business\_telephone %\> | \<%=h address.city %\> | \<%= link\_to 'Show', business %\> | \<%= link\_to 'Edit', edit\_business\_path(business) %\> | \<%= link\_to 'Destroy', business, :confirm =\> 'Are you sure?', :method =\> :delete %\> |

  

\<%= link\_to ‘New business’, new\_business\_path %\>

This is my business controller form:

def index  
@businesses = Business.search(params[:search])

```
respond_to do |format|
  format.html # index.html.erb
  format.xml { render :xml => @businesses }
end

```

end

def show  
@business = Business.find(params[:id])  
respond\_to do |format|  
format.html # show.html.erb  
format.xml { render :xml =\> @business }  
end  
end

What have I missed?

---

<div class="post-metadata">

**Author:** ![sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@sam](https://www.ruby-forum.com/u/sam)\
**Post date:** [February 19, 2010, 12:26am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it-error/183715/2 "2010-02-19T00:26:08Z")

</div>

On 18/02/2010 11:08 PM, Sam wrote:

> 21:  
> format.xml { render :xml =\> @businesses }
> 
> What have I missed?

Errm… I guess you’ve missed the fact that you haven’t actually loaded  
or assigned @addresses in your controller?

Cheers,  
Gary.

---

<div class="post-metadata">

**Author:** ![sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@sam](https://www.ruby-forum.com/u/sam)\
**Post date:** [February 26, 2010, 11:38am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it-error/183715/3 "2010-02-26T11:38:32Z")

</div>

Hi,

You are looping through the addresses this way  
\<% for address in @addresses %\>  
but your controller misses @addresses  
Please mention that in your controller.

Bye!!

---

<div class="post-metadata">

**Author:** ![sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@sam](https://www.ruby-forum.com/u/sam)\
**Post date:** [February 26, 2010, 11:58am UTC](https://www.ruby-forum.com/t/you-have-a-nil-object-when-you-didnt-expect-it-error/183715/4 "2010-02-26T11:58:27Z")

</div>

it looks that error coming in yours show.html.erb

create a test data fisrt in new.html.erb  
then GoTo show.html.erb

or in show.html.erb Put

\<%= if @object.nil? %\>

\<% there are No data in records %\>

\<%else%\>

yours code

\<% end %\>

ok if works then tell and if not then must reply…

On Fri, Feb 26, 2010 at 4:06 PM, Rails ROR [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > 21:
> > 
> > Business name \<%=h business.business\_name %\> respond\_to do |format| end .
> 
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Thanks:  
Rajeev sharma
