Commit 51bdd56c by Vy Quoc Vu

cart

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