Update quantity

Hi,

I added a field to my cart that allows you to update the quantity of
the item but when I try to update the quantity I get an error:

NoMethodError in CartController#update
undefined method `update_attribute’ for #Array:0x3464698

app/models/cart.rb:33:in update' app/controllers/cart_controller.rb:53:inupdate’
Request
Parameters: {“qty_update”=>“3”, “id”=>“2”}


I have the following tables:

  1. carts: id, customer_id
  2. cart_items: id, product_id, cart_id, price, amount, created_at

The cart class:
def update(product_id, qty)
ci = cart_items.find_all_by_product_id(product_id)
product = Product.find(product_id)
ci.update_attribute(:amount, qty.to_i)
return ci
end

The cart_controller:
def update
@product = Product.find(params[:id])
qty_update = params[:qty_update]
if request.post?
@cart.update(params[:id], qty_update)
flash[:notice] = “Updated #{product.title}
redirect_to :action => “view_cart”
end
end

Would anyone have an idea why updating the quantity doesn’t work?
And also, besides checking that the new quantity is bigger than 1,
should I test for anything else?

Thanks in advance,
Elle

cart_items.find_all_by_product_id(product_id)

will certainly return an array, not a cart item object… you might use

cart_items.find_all_by_product_id(product_id).first

bye

On Sun, 2007-10-21 at 03:41 -0700, elle wrote:

ci = cart_items.find_all_by_product_id(product_id)


Carlos Júnior
[email protected]
Boa Noite BH - Os melhores momentos da noite
+55 31 87635606

will certainly return an array, not a cart item object… you might use
cart_items.find_all_by_product_id(product_id).first

When I changed that, I now get this error:

NameError in CartController#update
undefined local variable or method `product’ for #<CartController:
0x31795c8>

app/controllers/cart_controller.rb:54:in `update’
Request
Parameters: {“qty_update”=>“4”, “id”=>“2”}

My cart_controller.rb is:
49 def update
50 @product = Product.find(params[:id])
51 qty_update = params[:qty_update]
52 if request.post?
53 @cart.update(params[:id], qty_update)
54 flash[:notice] = “Updated #{product.title}
55 redirect_to :action => “view_cart”
56 end
57 end

Any ideas?

TIA,
Elle

Actually, changing it from @product to product and then changing this
line: @cart.update(product, qty_update) – fixed it :slight_smile:

Thanks.
Elle

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs