Commit 688d4832 by vulehuan

card index page

parent 96fc05a1
...@@ -452,3 +452,6 @@ footer { ...@@ -452,3 +452,6 @@ footer {
font-weight: bold; font-weight: bold;
} }
} }
thead {
font-weight: bold;
}
\ No newline at end of file
class CardsController < ApplicationController class CardsController < ApplicationController
add_breadcrumb "Cards", :cards_path
def index def index
@card_infos = Hash.new
if session[:SHOPPING_CARD_SESSION_NAME] != nil
Please register or sign in to reply
@card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end
if params[:product_id] != nil
Please register or sign in to reply
# first time add to card
if @card_infos.empty?
product = Product.find(params[:product_id])
  • @huanvl Add exception if params[:product_id] nil or blank or not exist

    Edited
Please register or sign in to reply
card_items = Array.new
card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = Hash.new
@card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path
else
card_items = @card_infos[:card_items]
Please register or sign in to reply
card_items.each do |card_item, key|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
card_item[:quantity] += 1
card_items[key] = card_item
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path
end
end
# if a product not exist in card
card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = @card_infos[:customer_info]
@card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path
end
end
end end
def checkout def checkout
......
<h1>Cards#index</h1> <% provide(:title, "Cards") %>
<p>Find me in app/views/cards/index.html.erb</p> <div class="body-box">
<h2 class="sprite-2">
Cards<span class="sprite-2"></span>
</h2>
<form>
<table class="table table-hover">
<thead>
<tr>
<td>No.</td>
<td>Product name</td>
<td>Quantity</td>
<td>Price</td>
<td>Total</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<% if @card_infos.empty? %>
<tr><td colspan="6"><a href="<%= products_path %>">Click here to order</a></td></tr>
Please register or sign in to reply
<%
else
card_items = @card_infos[:card_items]
$i = 1
total = 0
card_items.each do |card_item|
product = Product.find(card_item[:product_id])
total += product.price
%>
<tr>
<td><%= $i %></td>
<td><%= link_to product.name, product_path(product) %></td>
<td><input type="text" class="form-control" value="<%= card_item[:quantity] %>" /></td>
<td><%= number_with_delimiter(product.price) %></td>
<td><%= number_with_delimiter(product.price * card_item[:quantity]) %></td>
<td>
<a href="" class="btn btn-default">Remove</a>
</td>
</tr>
<%
$i += 1
end
%>
<tr>
<td colspan="3">
</td>
<td colspan="3">
<b><%= number_with_delimiter(total) %></b>
</td>
</tr>
<tr>
<td colspan="6">
<div class="text-center">
<input class="btn btn-default" type="submit" name="btn[update]" value="Update quantity" />
<a class="btn btn-primary" href="">Check out</a>
</div>
</td>
</tr>
<%
end
%>
</tbody>
</table>
</form>
<div class="clearfix"></div>
</div>
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