Commit 76c37c83 by vulehuan

move card remove function to new action

parent 79806f7d
...@@ -8,26 +8,9 @@ class CardsController < ApplicationController ...@@ -8,26 +8,9 @@ class CardsController < ApplicationController
end end
unless params[:product_id].blank? unless params[:product_id].blank?
if Product.where(id: params[:product_id]).exists? if Product.where(id: params[:product_id]).exists?
# if remove a product from card
if !params[:card_action].blank? && params[:card_action] == 'remove' if !params[:card_action].blank? && params[:card_action] == 'remove'
# Nothing to delete
if @card_infos.empty?
redirect_to cards_path and return
else
card_items = @card_infos[:card_items]
found = false
card_items.each do |card_item|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
card_items.delete(card_item)
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
found = true
end
flash[:error] = "Invalid request (product not exist in card)" unless found
redirect_to cards_path and return
end
end
elsif !params[:card_action].blank? && params[:card_action] == 'update' elsif !params[:card_action].blank? && params[:card_action] == 'update'
# Nothing to update # Nothing to update
if @card_infos.empty? if @card_infos.empty?
...@@ -91,6 +74,34 @@ class CardsController < ApplicationController ...@@ -91,6 +74,34 @@ class CardsController < ApplicationController
end end
end end
# if remove a product from card
def remove
@card_infos = {}
unless session[:SHOPPING_CARD_SESSION_NAME].blank?
@card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end
unless params[:product_id].blank?
# Nothing to delete
if @card_infos.empty?
redirect_to cards_path and return
else
card_items = @card_infos[:card_items]
found = false
card_items.each do |card_item|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
card_items.delete(card_item)
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
found = true
end
flash[:error] = "Invalid request (product not exist in card)" unless found
redirect_to cards_path and return
end
end
end
redirect_to cards_path and return
end
def checkout def checkout
add_breadcrumb "Checkout", url_for(action: 'checkout') add_breadcrumb "Checkout", url_for(action: 'checkout')
@card_infos = {} @card_infos = {}
......
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
<%= link_to(cards_path(product_id: card_item[:product_id], card_action: 'update'), class: 'btn btn-info btn-update-card-quantity') do %> <%= link_to(cards_path(product_id: card_item[:product_id], card_action: 'update'), class: 'btn btn-info btn-update-card-quantity') do %>
<span class="glyphicon glyphicon-plus"></span> Update quantity <span class="glyphicon glyphicon-plus"></span> Update quantity
<% end %> <% end %>
<%= link_to(cards_path(product_id: card_item[:product_id], card_action: 'remove'), class: 'btn btn-danger', dada: '{:confirm=>"Do you want to remove this product?"}') do %> <%= link_to(url_for(controller: 'cards', action: 'remove', product_id: card_item[:product_id]), class: 'btn btn-danger', dada: '{:confirm=>"Do you want to remove this product?"}') do %>
<span class="glyphicon glyphicon-remove"></span> Remove <span class="glyphicon glyphicon-remove"></span> Remove
<% end %> <% end %>
</div> </div>
......
...@@ -10,6 +10,7 @@ VenshopApp::Application.routes.draw do ...@@ -10,6 +10,7 @@ VenshopApp::Application.routes.draw do
get "cards/confirm_checkout" get "cards/confirm_checkout"
post "cards/confirm_checkout" post "cards/confirm_checkout"
get "cards/thankyou" get "cards/thankyou"
get "cards/remove"
resources :users resources :users
resources :sessions, only: [:new, :create, :destroy] resources :sessions, only: [:new, :create, :destroy]
resources :products resources :products
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment