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
On 10/15/07, [email protected] [email protected]
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/
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.
[email protected] 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?
[email protected] 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