ruby on rails - Add multiple items to a cart at the same time -


i have following relations :

class cartitem belongs_to :cart belongs_to :product  class product (<=> category) has_many :cart_items   class cart has_many :cart_items 

when adds product cart, creates line in cartitem table cart.id , product.id. can add et remove product cart on edit page, works.

but add or remove more 1 product cart @ same time. need input number field when customer put number want add/remove. however, don't manage because in edit form, if put field called example "number" (for each product) error appear because there no attribute "number". should add "field_tag" how can work ? in advance

i work on ecommerce gem written rails called spree. here's how we've solved problem.

we have 4 models: variant, product, order , lineitem. these form basics of our ordering system. variant can considered "mutation" of product. have tshirt come in red, green or blue. product tshirt, while colours variants.

variants linked orders way of lineitems. lineitem object stores: variant_id, quantity , current price of variant, in case changes later. don't want prices changing on users unexpectedly!

the logic adding item cart form variant_id , quantity field on product's page. here's spree's version. controller's action deals form takes variant_id , quantity, , this:

  1. checks if order exists
  2. if order doesn't exist, creates one
  3. creates new line item on order quantity , variant_id specified, , stores price.

spree's voodoo around little more complex care inventory levels , on, that's basic gist of it.

when user goes view cart, present them form line items , quantity numeric field. form looks this:

checkout http://f.cl.ly/items/3p3t3o1f283a1e0z3e0c/screen%20shot%202013-07-11%20at%208.39.20%20am.png

the code produce form in 3 files: orders/edit.html.erb, orders/_form.html.erb , orders/_line_item.html.erb

that form works submitting orderscontroller#update, , due fields in form being nested attributes, entire order , line items updated suitably.

i hope helps you!


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -