Skip to content

Commit 060bece

Browse files
committed
Fixed reduce quantity bug
1 parent f0a3877 commit 060bece

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

app/controllers/line_items_controller.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class LineItemsController < ApplicationController
22
before_action :logged_in_user, only: %i[create destroy add_quantity reduce_quantity]
33

44
def create
5-
# if(@order.nil?)
6-
# @order = Order.new(order_params)
5+
# if(@order.nil?)
6+
# @order = Order.new(order_params)
77

88
# Find associated product and current cart
99
chosen_product = Product.find(params[:product_id])
@@ -45,9 +45,13 @@ def add_quantity
4545

4646
def reduce_quantity
4747
@line_item = LineItem.find(params[:id])
48-
@line_item.quantity -= 1 if @line_item.quantity > 1
49-
@line_item.save
50-
redirect_to cart_path(@current_cart)
48+
if @line_item.quantity > 1
49+
@line_item.quantity -= 1
50+
@line_item.save
51+
redirect_to cart_path(@current_cart)
52+
elsif @line_item.quantity == 1
53+
destroy
54+
end
5155
end
5256

5357
private

app/models/line_item.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class LineItem < ApplicationRecord
22
belongs_to :product
3-
belongs_to :cart , dependent: :destroy
3+
belongs_to :cart
44
# belongs_to :order
55

66
# LOGIC

config/routes.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
resources :microposts, only: %i[create destroy]
2121

2222
# About Shopping cart
23-
# get 'carts/:id' => 'carts#show', as: 'cart'
2423
get 'carts/:id' => 'carts#show', as: 'cart'
25-
# get 'carts' => 'carts#show', as: 'cart'
2624
delete 'carts/:id' => 'carts#destroy'
2725

2826
post 'line_items/:id/add' => 'line_items#add_quantity', as: 'line_item_add'

0 commit comments

Comments
 (0)