06 Apr 2009, 00:39
Generic-user-small

Antoine W. Solomon Jr (5 posts)

Hello all,

I get an ArgumentError with my cart.rb and store_controller. I believe everything is working correctly.

ArgumentError in StoreController#add_to_cart

wrong number of arguments (1 for 0)

/home/asolomon15/work/depot/app/models/cart.rb:14:in initialize' /home/asolomon15/work/depot/app/models/cart.rb:14:in new’ /home/asolomon15/work/depot/app/models/cart.rb:14:in add_product' /home/asolomon15/work/depot/app/controllers/store_controller.rb:11:in add_to_cart’

Anyone run into this before?

Regards, Antoine

06 Apr 2009, 04:42
Generic-user-small

John Doe (1 post)

I am getting a NoMethodError in StoreController#add_to_cart

I think it have to do with one of these two files but I cant get it working

store_contoller.rb

class StoreController < ApplicationController
  def index
  	@products = Product.find_products_for_sale
  end
   
  def add_to_cart
  	product = Product.find(params[:id])
  	@cart = find_cart
  	@cart.add_product(product)
  end
  
private

	def find_cart 
		session[:cart] ||= Cart.new 
	end 

end
cart.rb
`
class Cart
	attr_reader :items
	
	def initialize 
		@items = [] 
	end 
	
	def add_product(product)
		@items << product
	end
end
`
</pre
06 Apr 2009, 12:08
Generic-user-small

Antoine W. Solomon Jr (5 posts)

Did you try using the rake db:sessions:clear or the rake tmp:sessions:clear commands?

06 Apr 2009, 21:14
Generic-user-small

Antoine W. Solomon Jr (5 posts)

Did anyone get the above error before ?

06 Apr 2009, 21:27
Samr_small_pragsmall

Sam Ruby (584 posts)

Antoine: what does line 14 in app/models/cart.rb look like?

Just a guess, but does it look like this:

@current_item = Cart.new(product)@

When it should look something like this:

@current_item = CartItem.new(product)@

But that’s just a guess. All I can tell from what you’ve told me is that you are calling @new@ on a class which is not expecting an argument.

06 Apr 2009, 22:15
Generic-user-small

Antoine W. Solomon Jr (5 posts)

Sam,

Thanks for responding. I believe the CartItem is getting assigned to the @items array but not sure why I get this error “ArgumentError in StoreController#add_to_cart”.

Here are my classes.

Cart.rb

class Cart attr_reader :items

def initialize @items = [] end

def add_product(product) current_item = @items.find {|item| item.product == product} if current_item current_item.increment_quantity else @items « CartItem.new(product) end end

end

cart_item.rb

class CartItem attr_reader :product, :quantity

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

def increment_quantity
	@quantity += 1
end

def title
	@product.title
end

def price
	@product.price * @quantity
end end

store_controller.rb

class StoreController < ApplicationController def index @products = Product.find_products_for_sale end

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

private

def find_cart session[:cart] ||= Cart.new end end

06 Apr 2009, 22:48
Generic-user-small

Antoine W. Solomon Jr (5 posts)

Looking at my post I realized that My initialize method actually says intialize :-).

Sorry for the confusion all..

Highest regards, Antoine

10 Aug 2009, 17:50
Generic-user-small

Nate Plante (1 post)

I’m glad i stumbled upon this thread, i was getting so frustrated yesterday, and it turns out i had the exact same issue as you. same misspelling in the same file.

  You must be logged in to comment