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

**URL:** <https://www.ruby-forum.com/t/error-you-have-a-nil-object-when-you-didnt-expect-it/113537>\
**Category:** Rails\
**Created:** [September 1, 2007, 10:06am UTC](https://www.ruby-forum.com/t/error-you-have-a-nil-object-when-you-didnt-expect-it/113537 "2007-09-01T10:06:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [September 1, 2007, 10:06am UTC](https://www.ruby-forum.com/t/error-you-have-a-nil-object-when-you-didnt-expect-it/113537/1 "2007-09-01T10:06:22Z")

</div>

Hi everyone,

Very new to rails and while following the book: Beginning Ruby on  
Rails E-Commerce", I’m trying ti implement a cart, but the cart  
doesn’t appear on the html page at all. The code for the cart in app/  
views/layouts/application.rhtml:

\<% if @cart %\>

 \<%= render :partial =\> "cart/cart" %\>
\<%= drop\_receiving\_element("shopping\_cart", :url =\> { :controller =\> "cart", :action =\> "add" }) %\> \<% end %\>

If I take out the if @cart, I get this error:

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.cart\_items

Extracted source (around line #7):

4:  
5:

### Your Shopping Cart
  
6: 
  
7: \<% for item in @cart.cart\_items %\>  
8: -   
9: \<%= render :partial =\> “cart/item”, :object =\> item %\>  
10: 

This code appears on app/views/cart/\_cart.rhtml

Please help. Anyone?

TIA,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [September 1, 2007, 3:34pm UTC](https://www.ruby-forum.com/t/error-you-have-a-nil-object-when-you-didnt-expect-it/113537/2 "2007-09-01T15:34:09Z")

</div>

You have to declare the @cart variable in the controller.

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [September 2, 2007, 7:30am UTC](https://www.ruby-forum.com/t/error-you-have-a-nil-object-when-you-didnt-expect-it/113537/3 "2007-09-02T07:30:23Z")

</div>

On Sep 2, 12:33 am, “Thomas W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> You have to declare the @cart variable in the controller.

Thanks for the quick reply. Here is my cart\_controller.rb:

class CartController \< ApplicationController  
before\_filter :initialize\_cart

def add  
params[:id].gsub!(/product\_/, “”)  
@product = Product.find(params[:id])

```
if request.xhr?
  @item = @cart.add(params[:id])
  flash.now[:cart_notice] = "Added <em>#{@item.product.title}</

```

em\>"  
render :action =\> “add\_with\_ajax”  
elsif request.post?  
@item = @cart.add(params[:id])  
flash[:cart\_notice] = “Added _#{@item.product.title}_”  
redirect\_to :controller =\> “catalog”  
else  
render  
end  
end  
…

and my cart.rb model:

class Cart \< ActiveRecord::Base  
has\_many :cart\_items  
has\_many :products, :through =\> :cart\_items

def add(product\_id)  
items = cart\_items.find\_all\_by\_product\_id(product\_id)  
product = Product.find(product\_id)  
if items.size \< 1  
ci = cart\_items.create(:product\_id =\> product\_id,  
:amount =\> 1,  
:price =\> product.price)  
else  
ci = items.first  
ci.update\_attribute(:amount, ci.amount + 1)  
end  
ci  
end  
…

and my cart\_item.rb model:

class CartItem \< ActiveRecord::Base  
belongs\_to :cart  
belongs\_to :product  
end

What am I doing wrong?

TIA,  
Elle

---

<div class="post-metadata">

**Author:** ![elle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ccd318/32.png) [@elle](https://www.ruby-forum.com/u/elle)\
**Post date:** [September 3, 2007, 5:21pm UTC](https://www.ruby-forum.com/t/error-you-have-a-nil-object-when-you-didnt-expect-it/113537/4 "2007-09-03T17:21:00Z")

</div>

On Sep 2, 6:28 am, elle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def add  
> flash[:cart\_notice] = “Added _#removed\_email\_address@domain.invalid}_”  
> has\_many :cart\_items  
> ci = items.first  
> belongs\_to :product  
> end
> 
> What am I doing wrong?
> 
> TIA,  
> Elle

Hi Elle,

What code do you have for the initialize\_cart method in the  
CartController?

Cheers,  
Louis.
