Guest User

Untitled

a guest
Jun 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. ## cart
  2.  
  3. class Cart
  4. has_many :products
  5. accepts_nested_attributes_for :products
  6. end
  7.  
  8. ## controller
  9.  
  10. class CartController < ApplicationController
  11. def update
  12. delete_zero_quantity_params
  13. @cart.update_attributes params[:cart]
  14. @cart.save
  15. debugger # see below
  16. end
  17.  
  18. private
  19.  
  20. def delete_zero_quantity_params
  21. if params['cart']['products_attributes']
  22. params['cart']['products_attributes'].map do |i, product|
  23. product['_delete'] = true if product[:quantity].to_i == 0
  24. end
  25. end
  26. end
  27. end
  28.  
  29. ## debugger
  30.  
  31. params
  32. # {"cart"=>{"products_attributes"=>{"0"=>{"_delete"=>true, "quantity"=>"0", "id"=>"1"}}}, "action"=>"update", "controller"=>"cart"}
  33. @cart.products
  34. # [#<Product id: 1, design_id: 1, order_id: 1, quantity: 1, amount: #<BigDecimal:33402e4,'0.995E1',8(20)>, state: "cart", created_at: "2009-05-23 16:37:31", updated_at: "2009-05-23 16:42:44">]
  35.  
  36. @cart.products[0].attributes = params[:cart][:products_attributes]['0']
  37. # {"_delete"=>true, "quantity"=>"0", "id"=>"1"}
  38. @cart.products[0].marked_for_destruction?
  39. # nil
  40.  
  41. ## Question [plain_text]
  42.  
  43. In the debugger, quantity:1 is expected because I normalize it for a
  44. "minimum order" in the product model. Regardless, the whole thing
  45. should have just been deleted.
  46.  
  47. Any ideas why the product wasn't deleted from the cart?
Add Comment
Please, Sign In to add comment