Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## cart
- class Cart
- has_many :products
- accepts_nested_attributes_for :products
- end
- ## controller
- class CartController < ApplicationController
- def update
- delete_zero_quantity_params
- @cart.update_attributes params[:cart]
- @cart.save
- debugger # see below
- end
- private
- def delete_zero_quantity_params
- if params['cart']['products_attributes']
- params['cart']['products_attributes'].map do |i, product|
- product['_delete'] = true if product[:quantity].to_i == 0
- end
- end
- end
- end
- ## debugger
- params
- # {"cart"=>{"products_attributes"=>{"0"=>{"_delete"=>true, "quantity"=>"0", "id"=>"1"}}}, "action"=>"update", "controller"=>"cart"}
- @cart.products
- # [#<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">]
- @cart.products[0].attributes = params[:cart][:products_attributes]['0']
- # {"_delete"=>true, "quantity"=>"0", "id"=>"1"}
- @cart.products[0].marked_for_destruction?
- # nil
- ## Question [plain_text]
- In the debugger, quantity:1 is expected because I normalize it for a
- "minimum order" in the product model. Regardless, the whole thing
- should have just been deleted.
- Any ideas why the product wasn't deleted from the cart?
Add Comment
Please, Sign In to add comment