Commit 51bdd56c by Vy Quoc Vu

cart

parent 0e984652
class CartProductsController < ApplicationController class CartProductsController < ApplicationController
before_action :call_category , only: [:new, :index] before_action :call_category , only: [:new, :index]
include CategoriesHelper include CategoriesHelper
def add def add
id = params[:id] id = params[:id]
if session[:cart] then if session[:cart] then
......
class CartsController < ApplicationController class CartsController < ApplicationController
include CategoriesHelper include CategoriesHelper
def show def show
if user_signed_in?
@show_cart = Cart.where(user_id: current_user.id)
end
end end
def create def create
if !session[:cart].nil?
new_cart = Cart.new(cart_params) new_cart = Cart.new(cart_params)
new_cart.total_price = calculate_total_price
new_cart.status = "In process"
if user_signed_in?
new_cart.user_id = current_user.id
end
new_cart.save new_cart.save
byebug byebug
session[:cart].each do |id, quantity|
cart_product = CartProduct.new
cart_product.cart_id = new_cart.id
cart_product.product_id = id
cart_product.price = Product.find_by_id(id).price
cart_product.quantity = quantity
cart_product.save
end end
flash[:success] = "Success!"
def info session[:cart] = nil
@cart = Cart.new end
redirect_to :action => "show"
end end
def user_params def info
params.require(:cart).permit(:name, :email, :address)
end end
def cart_params def cart_params
params.require(:session).permit(:name, :mail, :address) params.require(:session).permit(:name, :mail, :address)
end end
private
def calculate_total_price
total = 0
if !session[:cart].nil?
session[:cart].each do |id, quantity|
product = Product.find_by_id(id)
if !product.nil?
total = total + product.price * quantity
end
end
end
total
end
end end
class ProductsController < ApplicationController class ProductsController < ApplicationController
include CategoriesHelper include CategoriesHelper
def index def index
@products = Product.paginate(page: params[:page], :per_page => 50) @products = Product.paginate(page: params[:page], :per_page => 50)
end end
def show def show
......
class Cart < ActiveRecord::Base class Cart < ActiveRecord::Base
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :mail, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
belongs_to :user
has_many :cart_product has_many :cart_product
end end
class User < ActiveRecord::Base class User < ActiveRecord::Base
has_many :carts
# Include default devise modules. Others available are: # Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable # :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, devise :database_authenticatable, :registerable,
......
<h1>Cart Product</h1> <h1>Cart Products</h1>
<ul> <ul>
<% total = 0 %> <% total = 0 %>
<% if !cart_product.nil? %>
<% cart_product.each do |id, quantity| %> <% cart_product.each do |id, quantity| %>
<% product = Product.find_by_id(id) %> <% product = Product.find_by_id(id) %>
<% if !product.nil? %> <% if !product.nil? %>
...@@ -10,7 +11,10 @@ ...@@ -10,7 +11,10 @@
<%= link_to truncate(product.name, length:15), "/products/#{product.id}" %> <%= link_to truncate(product.name, length:15), "/products/#{product.id}" %>
<%= quantity %> <%= quantity %>
</li> </li>
<% end %>
<% end -%> <% end -%>
<% else %>
<h3>Empty</h3>
<% end -%> <% end -%>
<br> <br>
<br> <br>
......
<h1>Your Cart</h1> <h1>Your Cart</h1>
<%= link_to "Empty Cart" ,cart_products_clear_path %> <%= link_to "Empty Cart" ,'/cart_products/clear' %>
<ul> <ul>
<% total = 0 %> <% total = 0 %>
<% if !@cart_product.nil? %>
<% @cart_product.each do |id, quantity| %> <% @cart_product.each do |id, quantity| %>
<% product = Product.find_by_id(id) %> <% product = Product.find_by_id(id) %>
<% if !product.nil? %> <% if !product.nil? %>
...@@ -13,6 +14,9 @@ ...@@ -13,6 +14,9 @@
</div> </div>
<% end %> <% end %>
<% end -%> <% end -%>
<% else %>
<h3>Empty</h3>
<% end -%>
</ul> </ul>
<div class="col-md-12"> <div class="col-md-12">
<nav> <nav>
......
<h1>Cart info</h1> <h1>Cart info</h1>
<ul> <ul>
<% total = 0 %> <% @total = 0 %>
<% cart_product.each do |id, quantity| %> <% cart_product.each do |id, quantity| %>
<% product = Product.find_by_id(id) %> <% product = Product.find_by_id(id) %>
<% if !product.nil? %> <% if !product.nil? %>
<% total = total + product.price * quantity %> <% @total = @total + product.price * quantity %>
<li> <li>
<%= link_to product.name, "/products/#{product.id}" %> <%= link_to product.name, "/products/#{product.id}" %>
</br> </br>
...@@ -13,22 +13,21 @@ ...@@ -13,22 +13,21 @@
<% end -%> <% end -%>
<% end -%> <% end -%>
<br> <br>
<h2> <%= number_to_currency(total/100.to_f, :unit => '$')%> </h2> <h2> <%= number_to_currency(@total/100.to_f, :unit => '$')%> </h2>
</ul> </ul>
<div class="row"> <div class="row">
<div class="col-md-10 "> <div class="col-md-10 ">
<%= form_for(:session, url: create_cart_path) do |f| %> <%= form_for(:session, url: create_cart_path) do |f| %>
<%= f.label :email %> <%= f.label :mail %>
<%= f.email_field :email, class: 'form-control' %> <%= f.email_field :mail, class: 'form-control' %>
<%= f.label :name %> <%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %> <%= f.text_field :name, class: 'form-control' %>
<%= f.label :address %> <%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %> <%= f.text_field :address, class: 'form-control' %>
</br> </br>
<%= f.submit "Log in", class: "btn btn-primary" %> <%= f.submit "Submit" , class: "btn btn-primary" %>
<% end %> <% end %>
</div> </div>
</div> </div>
<h1>show cart</h1>
<ul>
<% if !@show_cart.nil? %>
<% @show_cart.each do |cart| %>
<h4>
<%= cart.id.to_s + " | "%>
<%= cart.mail.to_s + " | " %>
<%= cart.name.to_s + " | " %>
<%= cart.total_price.to_s + " | " %>
<%= cart.status.to_s + " | " %>
<%= cart.address %>
</h4>
<% end -%>
<% else -%>
<h2> Your cart Empty </h2>
<% end -%>
</ul>
...@@ -4,17 +4,18 @@ ...@@ -4,17 +4,18 @@
get 'login' => 'sessions#new' get 'login' => 'sessions#new'
get 'category/:id' => 'category#show' get 'category/:id' => 'category#show'
get 'categories/products/:id' => 'products#show' get 'products/:id' => 'products#show'
get 'cart_products/:id' => 'cart_products#add' get 'cart_products/:id' => 'cart_products#add'
get 'cart_products/clear' => 'cart_products#clear_cart_product' get 'cart_products/clear' => 'cart_products#clear_cart_product'
get 'cart' => 'carts#info' get 'cart' => 'carts#info'
get 'carts' => 'carts#new' get 'carts' => 'carts#new'
get 'carts/show' =>'carts#show'
post 'carts/create' => 'carts#create', as: 'create_cart' post 'carts/create' => 'carts#create', as: 'create_cart'
resources :categories, only: [:index, :show] resources :categories, only: [:index, :show]
resources :products, only: [:index, :show] resources :products, only: [:index, :show]
resources :carts, only: [:info] resources :carts, only: [:info,:destroy]
resources :cart_products, only: [:new, :edit, :update, :index] resources :cart_products, only: [:new, :edit, :update, :index]
root "products#index" root "products#index"
# get 'cate/:id' => 'cacate#show' # get 'cate/:id' => 'cacate#show'
......
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