increase items in shopping cart using rails and redis -
i having trouble increasing quantity of items in cart. add one, switches 'remove' button. there way can reconfigure can add more 1 of each item?
carts_controller.rb
def add $redis.sadd current_user_cart, params[:item_id] render json: current_user.cart_count, status: 200 end
carts.coffee
$(window).load -> $('a[data-target]').click (e) -> e.preventdefault() $this = $(this) if $this.data('target') == 'add to' url = $this.data('addurl') new_target = "remove from" else url = $this.data('removeurl') new_target = "add to" $.ajax url: url, type: 'put', success: (data) -> $('.cart-count').html(data) $this.find('span').html(new_target) $this.data('target', new_target) $('#mycart .remove').click (e) -> e.preventdefault() $this = $(this).closest('a') url = $this.data('targeturl') $.ajax url: url, type: 'put', success: (data) -> $('.cart-count').html(data) $this.closest('.cart-item').slideup()
i figured out on own. items being saved array, more 1 can added cart. 'add to' button no longer toggling , forth between 'add to' , 'remove from'. 'remove' button separate.
def show cart_ids = $redis.lrange current_user_cart, -100, 100 @cart_items = item.find(cart_ids) end def add $redis.lpush current_user_cart, params[:item_id] render json: current_user.cart_count, status: 200 end def remove $redis.lrem current_user_cart, 1, params[:item_id] render json: current_user.cart_count, status: 200 end
Comments
Post a Comment