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:in `update'' 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 <em>#{product.title}</em>" 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 junior-S4GJ/JUYAA8bCsTw5SJnEQ@public.gmane.org Boa Noite BH - Os melhores momentos da noite +55 31 87635606 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> will certainly return an array, not a cart item object... you might use > cart_items.find_all_by_product_id(product_id).firstWhen 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 <em>#{product.title}</em>" 55 redirect_to :action => "view_cart" 56 end 57 end Any ideas? TIA, Elle On Oct 22, 9:54 am, Carlos Júnior <chjun...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 > jun...-S4GJ/JUYAA8bCsTw5SJnEQ@public.gmane.org > Boa Noite BH - Os melhores momentos da noite > +55 31 87635606--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Actually, changing it from @product to product and then changing this line: @cart.update(product, qty_update) -- fixed it :) Thanks. Elle On Oct 22, 2:59 pm, elle <wazne...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 <em>#{product.title}</em>" > 55 redirect_to :action => "view_cart" > 56 end > 57 end > > Any ideas? > > TIA, > Elle > > On Oct 22, 9:54 am, Carlos Júnior <chjun...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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 > > jun...-S4GJ/JUYAA8bCsTw5SJnEQ@public.gmane.org > > Boa Noite BH - Os melhores momentos da noite > > +55 31 87635606--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---