# Yet another problem with NoMethodError in Store#display\_cart

**URL:** <https://www.ruby-forum.com/t/yet-another-problem-with-nomethoderror-in-store-display-cart/64271>\
**Category:** Rails\
**Created:** [June 23, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/yet-another-problem-with-nomethoderror-in-store-display-cart/64271 "2006-06-23T17:58:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b2d939/32.png) [@erix](https://www.ruby-forum.com/u/erix)\
**Post date:** [June 23, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/yet-another-problem-with-nomethoderror-in-store-display-cart/64271/1 "2006-06-23T17:58:51Z")

</div>

Hi everybody,

I’ve search a lot on google to help me with this one without success. I  
know that some people had the same problem as I have, but there  
solutions doesn’t seems to work for me.

I’m trying to do the depot example in the AWDwR book. But now I’m stock  
with a problem that I can’t solve. I get this error:

\<— begin error here —\>  
NoMethodError in Store#display\_cart

Showing app/views/store/display\_cart.rhtml where line #4 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.size

Extracted source (around line #4):

1:

# Display Cart
  
2:  
3: 

4: Your cart contains \<%= @items.size %\> items.  
5:

  
\<— end error here —\>

Here is a copy/paste of my code:

\<— file store\_controller.rb begin here —\>  
class StoreController \< ApplicationController

def index  
@products = Product.salable\_items  
end

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
redirect\_to(:action =\> ‘display\_cart’)  
end

def diplay\_cart  
@cart = find\_cart  
@items = @cart.items  
end

private  
def find\_cart  
session[:cart] ||= Cart.new  
end  
end  
\<— file store\_controller.rb end here —\>

\<— file application.rb begin here —\>  
class ApplicationController \< ActionController::Base  
model :cart  
model :line\_item  
end  
\<— file application.rb end here —\>

\<— file cart.rb begin here —\>  
class Cart  
attr\_reader :items  
attr\_reader :total\_price

```
def initialize
    @items = []
    @total_price = 0.0
end

def add_product(product)
    @items << LineItem.for_product(product)
    @total_price += product.price
end

```

end  
\<— file cart.rb end here —\>

\<— file line\_item.rb begin here —\>  
class LineItem \< ActiveRecord::Base  
belongs\_to :product

```
def self.for_product(product)
    item = self.new
    item.quantity = 1
    item.product = product
    item.unit_price = product.price
    item
end

```

end  
\<— file line\_item.rb end here —\>

\<— file display\_cart.rhtml begin here —\>

# Display Cart

Your cart contains \<%= @items.size %\> items.

\<--- file display\_cart.rhtml end here ---\>

Here is what I tried so far:

- Delete all the cache from my browser (I’m using Opera, but I also  
tried Firefox)
- Delete all the cookies
- Check for typos
- Restart WEBrick
- Reboot my computer (I’m using Windows XP with mySQL 5.0.18 and RoR  
1.2)

I have put some breakpoint() in my code. The ‘Cart’ is created, @items  
is set correctly and the session is also working (inside  
store\_controller.rb). If I put a breakpoint() method inside the  
‘display\_cart.rhtml’, at the prompt, I can see that the 'session[:cart]  
variable is properly set, but the ‘@items’ is set to ‘nil’. It really  
seems like the ‘@items’ is not readable or not set from within the  
‘display\_cart.rhtml’ file. But I think that the ‘:model cart’ line is  
correctly set in my ‘application.rb’.

Can somebody help me with this one?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![erix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b2d939/32.png) [@erix](https://www.ruby-forum.com/u/erix)\
**Post date:** [June 23, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/yet-another-problem-with-nomethoderror-in-store-display-cart/64271/2 "2006-06-23T18:14:39Z")

</div>

On Jun 23, 2006, at 10:58 AM, erix wrote:

> 1:
> 
> # Display Cart
>   
> 2:  
> 3: 
> 
> 4: Your cart contains \<%= @items.size %\> items.  
> 5:
> 
>   
> \<— end error here —\>

Try changing the #display\_cart method to:

def diplay\_cart  
@cart = find\_cart  
@items = @cart.items || []  
end

This will solve the problem when @cart.items returns nil. However, f  
you previously called #add\_to\_cart and #display\_cart still doesn’t  
display the items you added, then there’s a bug elsewhere.

cr

---

<div class="post-metadata">

**Author:** ![erix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b2d939/32.png) [@erix](https://www.ruby-forum.com/u/erix)\
**Post date:** [June 23, 2006, 6:39pm UTC](https://www.ruby-forum.com/t/yet-another-problem-with-nomethoderror-in-store-display-cart/64271/3 "2006-06-23T18:39:36Z")

</div>

Chuck R. wrote:

> On Jun 23, 2006, at 10:58 AM, erix wrote:
> 
> > 1:
> > 
> > # Display Cart
> >   
> > 2:  
> > 3: 
> > 
> > 4: Your cart contains \<%= @items.size %\> items.  
> > 5:
> > 
> >   
> > \<— end error here —\>
> 
> Try changing the #display\_cart method to:
> 
> def diplay\_cart  
> @cart = find\_cart  
> @items = @cart.items || []  
> end
> 
> This will solve the problem when @cart.items returns nil. However, f  
> you previously called #add\_to\_cart and #display\_cart still doesn’t  
> display the items you added, then there’s a bug elsewhere.
> 
> cr

Hi Chuck R.,

I tried your code, but it wasn’t working either. I was still getting the  
same error. But I finally found the problem: a typo! Look at the name of  
the display\_cart method: ‘def diplay\_cart’ instead of ‘def  
display\_cart’. But you point me on the problem, because it wasn’t  
logical to get the same error after setting @items to [] if @cart.items  
was nil. At last, the @items.size was support to return 0. So I looked  
again in the function and then I saw it.

Thanks again!

---

<div class="post-metadata">

**Author:** ![erix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b2d939/32.png) [@erix](https://www.ruby-forum.com/u/erix)\
**Post date:** [June 23, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/yet-another-problem-with-nomethoderror-in-store-display-cart/64271/4 "2006-06-23T20:32:01Z")

</div>

On Jun 23, 2006, at 11:39 AM, erix wrote:

> again in the function and then I saw it.  
> A typo! My eyes just glanced right over it… Good catch.

cr
