# NoMethodError in StoreController#add\_to\_cart -\> getting nil

**URL:** <https://www.ruby-forum.com/t/nomethoderror-in-storecontroller-add-to-cart-getting-nil/190461>\
**Category:** Rails\
**Created:** [June 21, 2010, 12:26am UTC](https://www.ruby-forum.com/t/nomethoderror-in-storecontroller-add-to-cart-getting-nil/190461 "2010-06-21T00:26:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![noloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@noloman](https://www.ruby-forum.com/u/noloman)\
**Post date:** [June 21, 2010, 12:26am UTC](https://www.ruby-forum.com/t/nomethoderror-in-storecontroller-add-to-cart-getting-nil/190461/1 "2010-06-21T00:26:53Z")

</div>

Hey everybody,

I am currently following the depot application from the book Web Agile  
Development with Rails (3rd Edition) but after creating the controller  
for the Store and the Cart model, I am always getting a nil product, no  
matter how many products I add to the database.  
I am very new with RoR (this only the page 110 of the book) and thus I  
am not very sure about what I could do to mitigate the problem.

Thanks a lot in advance!

---

<div class="post-metadata">

**Author:** ![noloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@noloman](https://www.ruby-forum.com/u/noloman)\
**Post date:** [June 21, 2010, 12:39am UTC](https://www.ruby-forum.com/t/nomethoderror-in-storecontroller-add-to-cart-getting-nil/190461/2 "2010-06-21T00:39:46Z")

</div>

Manuel Manuel wrote:

> Hey everybody,
> 
> I am currently following the depot application from the book Web Agile  
> Development with Rails (3rd Edition) but after creating the controller  
> for the Store and the Cart model, I am always getting a nil product, no  
> matter how many products I add to the database.  
> I am very new with RoR (this only the page 110 of the book) and thus I  
> am not very sure about what I could do to mitigate the problem.
> 
> Thanks a lot in advance!

The errors are both Cart.rb, line number 9:

def add\_product(product)  
-\> @items \<\< product  
end

and

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
-\> @cart.add\_product(product)  
end

Error:  
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.\<\<
