# Errors ... errors and errors

**URL:** <https://www.ruby-forum.com/t/errors-errors-and-errors/101324>\
**Category:** Rails\
**Created:** [August 4, 2006, 8:48am UTC](https://www.ruby-forum.com/t/errors-errors-and-errors/101324 "2006-08-04T08:48:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 4, 2006, 8:48am UTC](https://www.ruby-forum.com/t/errors-errors-and-errors/101324/1 "2006-08-04T08:48:03Z")

</div>

I bought the second edition of the ruby on rails, it seems to work fine  
now, did some adjustments to the code. But now I get a second error and  
I want to learn to understand to fix them myself. So here is the error.

ArgumentError in StoreController#add\_to\_cart  
wrong number of arguments (1 for 0)  
#{RAILS\_ROOT}/app/models/cart.rb:15:in `initialize' #{RAILS_ROOT}/app/models/cart.rb:15:in`add\_product’  
#{RAILS\_ROOT}/app/controllers/store\_controller.rb:19:in `add\_to\_cart’  
-e:4

Now, it says as far as I understand it, that it should need 0 arguments  
instead of 1. But that should be quite impossible, beacause the cart.rb  
has a def add\_product(product) as we shall see later. I do not  
understand why it gives me that error. Here is the store\_controller.rb:

[CODE]  
class StoreController \< ApplicationController

def index  
@products = Product.find\_products\_for\_sale  
#creates an instance variable that holds a list of all the  
#products in order to make it available to the code in the view  
#that will display the table  
#find\_products\_for\_sale() IS DEFINED IN product.rb  
end

def add\_to\_cart  
@cart = find\_cart #we use find\_cart method to find (or create)  
#a cart in this session  
@product = Product.find(params[:id]) #we use the params object  
#to get the id parameter  
#from the request and then  
#call Product model to find  
#the product with that id.  
@cart.add\_product(@product) #add this product to the cart  
end  
private  
def find\_cart  
unless session[:cart] #if there is no cart in sesssion add one  
session[:cart] = Cart.new #add a new one  
end  
session[:cart] #return existing or new cart  
end  
end

[CODE]

Also here is the cart.rb

[CODE]  
class Cart  
include Reloadable

attr\_reader :items

def initialize  
@items = []  
end

def add\_product(product)  
existing\_product = @items.find {|item| item.product == product}  
if existing\_product  
existing\_product.increment\_quantity  
else  
@items \<\< Cart.new(product)  
end  
end  
end

[CODE]

And here is the cart\_item.rb

[CODE]  
class CartItem  
include Reloadable  
attr\_reader :product, :quantity

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

def increment\_quantity  
@quantity += 1  
end

def title  
@product.title  
end

def price  
@product.price \* @quantity  
end

end

[CODE]

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 4, 2006, 9:50am UTC](https://www.ruby-forum.com/t/errors-errors-and-errors/101324/2 "2006-08-04T09:50:10Z")

</div>

Hi,

it’s the _add\_to\_cart_ method (not add\_product(argument)) that you are  
calling with one argument.

This method of your store controller takes _no_ argument so the ruby  
interpreter is complaining.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 4, 2006, 11:12am UTC](https://www.ruby-forum.com/t/errors-errors-and-errors/101324/3 "2006-08-04T11:12:43Z")

</div>

Where do I call this add\_to\_cart method without one argument? I only  
call it in index.rhtml, otherwise I see not.

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 4, 2006, 10:34am UTC](https://www.ruby-forum.com/t/errors-errors-and-errors/101324/4 "2006-08-04T10:34:21Z")

</div>

Jan P. wrote:

> Hi,
> 
> it’s the _add\_to\_cart_ method (not add\_product(argument)) that you are  
> calling with one argument.
> 
> This method of your store controller takes _no_ argument so the ruby  
> interpreter is complaining.
> 
> Cheers,  
> Jan

Understood, thanks a lot. Here is the code for the index.rhtml. The “:id  
=\> product” can be asumed to be the first parameter of the function? How  
can I fix this so it will work?

[CODE]

# Your Pragmatic Catalog

\<% for product in @products -%\>

  
 ![]()  

### \<%= h(product.title) %\>
  
\<%= product.description %\>  

### Pret: \<%= product.price %\> RON
  
\<%= button\_to “Add to cart”, :action =\> :add\_to\_cart, :id =\>  
product %\>  

  
\<% end %\>

[CODE]

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 4, 2006, 11:37am UTC](https://www.ruby-forum.com/t/errors-errors-and-errors/101324/5 "2006-08-04T11:37:58Z")

</div>

Jan P. wrote:

> Hi,
> 
> sorry - seems as if the problem happens on line 15 of the Cart class:  
> @items  
> \<\< Cart.new(product)
> 
> You’re initializing a new Cart with the argument product, but haven’t  
> got an  
> initialize method that takes a product as an argument.
> 
> Cheers,  
> Jan

new(attributes = nil) {|self if block\_given?| …}  
New objects can be instantiated as either empty (pass no construction  
parameter) or pre-set with attributes but not yet saved (pass a hash  
with key names matching the associated table column names). In both  
instances, valid attribute keys are determined by the column names of  
the associated table â?? hence you canâ??t have attributes that arenâ??t part  
of the table columns.

It can support attributes as far as I see her the new method. How would  
you write the application so it works?

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 4, 2006, 11:29am UTC](https://www.ruby-forum.com/t/errors-errors-and-errors/101324/6 "2006-08-04T11:29:29Z")

</div>

Hi,

sorry - seems as if the problem happens on line 15 of the Cart class:  
@items  
\<\< Cart.new(product)

You’re initializing a new Cart with the argument product, but haven’t  
got an  
initialize method that takes a product as an argument.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![mercurius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/c89c15/32.png) [@mercurius](https://www.ruby-forum.com/u/mercurius)\
**Post date:** [August 4, 2006, 12:05pm UTC](https://www.ruby-forum.com/t/errors-errors-and-errors/101324/7 "2006-08-04T12:05:36Z")

</div>

Finally, I replace Cart.new(product) with CartItem.new(product). Since  
the Class Cart does NOT support another argument, of course it gave me  
an error. But CartItem DOES support a new argument … so it works.

[CODE]  
class CartItem  
include Reloadable  
attr\_reader :product, :quantity

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

def increment\_quantity  
@quantity += 1  
end

def title  
@product.title  
end

def price  
@product.price \* @quantity  
end

end

[CODE]
