# ArgumentError in StoreController#add\_to\_cart: wrong number of arguments (1 for 0)

**URL:** <https://www.ruby-forum.com/t/argumenterror-in-storecontroller-add-to-cart-wrong-number-of-arguments-1-for-0/117265>\
**Category:** Rails\
**Created:** [October 15, 2007, 2:43pm UTC](https://www.ruby-forum.com/t/argumenterror-in-storecontroller-add-to-cart-wrong-number-of-arguments-1-for-0/117265 "2007-10-15T14:43:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 15, 2007, 2:43pm UTC](https://www.ruby-forum.com/t/argumenterror-in-storecontroller-add-to-cart-wrong-number-of-arguments-1-for-0/117265/1 "2007-10-15T14:43:23Z")

</div>

I am really new to Ruby and Rails but as you probably see I am trying  
to get the shopping card working from the book. I am getting this  
error but I really don’t know what to do. I have looked up several of  
topics that stated the same problem but as far as I am, nothing has  
helped me out yet. Is there something wrong with the Product model?

This is in the StoreController and I believe it is all good.

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

Thanks in advance

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 15, 2007, 5:38pm UTC](https://www.ruby-forum.com/t/argumenterror-in-storecontroller-add-to-cart-wrong-number-of-arguments-1-for-0/117265/2 "2007-10-15T17:38:03Z")

</div>

On 10/15/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> @cart = find_cart
> product = Product.find(params[:id])
> @cart.add_product(product)
> 
> ```
> 
> end

Well, let’s see, the title of your post is “ArgumentError in  
StoreController#add\_to\_cart, wrong number of arguments (1 for 0).” I  
assume that this is because that’s the error you are seeing.

So what could that mean? It means that someone is calling  
StoreController#add\_to\_cart with an argument, but add\_to\_cart doesn’t  
accept any arguments.

The problem isn’t in the definition of add\_to\_cart, controller action  
methods don’t take ruby arguments. So…

Somewhere some other code. A view? A model method, some other  
controller method is calling add\_to\_cart with a parameter.

That’s why the error page should show a stack trace, doesn’t it? That  
should point you to the code which called add\_to\_cart which is the  
code which needs to be fixed.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [October 16, 2007, 1:48pm UTC](https://www.ruby-forum.com/t/argumenterror-in-storecontroller-add-to-cart-wrong-number-of-arguments-1-for-0/117265/3 "2007-10-16T13:48:10Z")

</div>

Hey Rick,

First of all, thank you for your reply, it finally works!  
So what did I do?I analyzed the stack trace and concluded that the  
problem was situated in the cart.rb  
At line 13:

```
  @items << CartItem.new(product)

```

It could not assign a new product to the CartItem model so I looked at  
the cart\_item.rb:  
The “initialize” - which stands for constructor in Ruby I guess - was  
spelled wrong and that was my problem! Now it seems to work properly.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 11, 2009, 11:09am UTC](https://www.ruby-forum.com/t/argumenterror-in-storecontroller-add-to-cart-wrong-number-of-arguments-1-for-0/117265/4 "2009-03-11T11:09:08Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Hey Rick,
> 
> First of all, thank you for your reply, it finally works!  
> So what did I do?I analyzed the stack trace and concluded that the  
> problem was situated in the cart.rb  
> At line 13:
> 
> ```
> @items << CartItem.new(product)
> 
> ```
> 
> It could not assign a new product to the CartItem model so I looked at  
> the cart\_item.rb:  
> The “initialize” - which stands for constructor in Ruby I guess - was  
> spelled wrong and that was my problem! Now it seems to work properly.

Ok! And

Well! And if this line and listed all above is present, but the error  
remains?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 15, 2010, 11:07am UTC](https://www.ruby-forum.com/t/argumenterror-in-storecontroller-add-to-cart-wrong-number-of-arguments-1-for-0/117265/5 "2010-06-15T11:07:00Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I am really new to Ruby and Rails but as you probably see I am trying  
> to get the shopping card working from the book. I am getting this  
> error but I really don’t know what to do. I have looked up several of  
> topics that stated the same problem but as far as I am, nothing has  
> helped me out yet. Is there something wrong with the Product model?
> 
> This is in the StoreController and I believe it is all good.
> 
> def add\_to\_cart  
> @cart = find\_cart  
> product = Product.find(params[:id])  
> @cart.add\_product(product)  
> end
> 
> Thanks in advance

edit in cart\_item.rb

// before  
def initialize  
@product = product  
@quantity = 1  
end

// after  
def initialize(product)  
@product = product  
@quantity = 1  
end
