File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11class LineItem < ApplicationRecord
22 belongs_to :product
3- belongs_to :cart , dependent : :destroy
3+ belongs_to :cart
44 # belongs_to :order
55
66 # LOGIC
Original file line number Diff line number Diff line change 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'
You can’t perform that action at this time.
0 commit comments