Commit 8912f75e by Nguyen Quoc Kien

Fix code

parent 9d557a5d
...@@ -6,17 +6,11 @@ class CartProductsController < ApplicationController ...@@ -6,17 +6,11 @@ class CartProductsController < ApplicationController
product = Product.find(params[:product_id]) product = Product.find(params[:product_id])
if check_quantity? if check_quantity?
add_product_to_cart(product.id.to_i, params[:quantity].to_i ) add_product_to_cart(product.id.to_i, params[:quantity].to_i )
respond_to do |format| redirect_to cart_path(id: @user_id)
format.html { redirect_to cart_path(id: @user_id), flash[:success] = 'Products add to cart'
notice: 'Products add to cart' }
format.json { head :no_content }
end
else else
respond_to do |format| redirect_to cart_path(id: @user_id)
format.html { redirect_to cart_path(id: @user_id), flash[:success] = 'Errors: Quantity'
notice: 'Errors: Quantity' }
format.json { head :no_content }
end
end end
end end
...@@ -44,9 +38,11 @@ class CartProductsController < ApplicationController ...@@ -44,9 +38,11 @@ class CartProductsController < ApplicationController
end end
def check_quantity? def check_quantity?
if ( params[:quantity].to_i && params[:quantity].to_i > 0 ) if params[:quantity].to_i > 0
return true return true
else false else
return false
end end
end end
end end
class CartsController < ApplicationController class CartsController < ApplicationController
#before_action :find_card, only: [ :create ]
def update
end
def new def new
@cart = Cart.new @cart = Cart.new
end end
def create def create
total = 0 total = 0
@cart = Cart.new(cart_params) @cart = Cart.new(cart_params)
@cart.save @cart.save
if current_user if current_user
...@@ -20,7 +14,6 @@ class CartsController < ApplicationController ...@@ -20,7 +14,6 @@ class CartsController < ApplicationController
@cart.update(user_id: "", status: "Checkout") @cart.update(user_id: "", status: "Checkout")
user_id = 'guess' user_id = 'guess'
end end
if @cart.save if @cart.save
session[user_id].each do |key, value| session[user_id].each do |key, value|
@product = Product.find(key) @product = Product.find(key)
...@@ -60,10 +53,6 @@ class CartsController < ApplicationController ...@@ -60,10 +53,6 @@ class CartsController < ApplicationController
params.require(:cart).permit(:full_name, :email, :address, :phone) params.require(:cart).permit(:full_name, :email, :address, :phone)
end end
def find_card
@cart = Cart.find(params[:id])
end
def update_info_user def update_info_user
if user_signed_in? if user_signed_in?
user = User.find(current_user.id) user = User.find(current_user.id)
......
...@@ -6,14 +6,10 @@ class ProductsController < ApplicationController ...@@ -6,14 +6,10 @@ class ProductsController < ApplicationController
@categories = Category.all @categories = Category.all
end end
# GET /products/:id
def show def show
@categories = Category.all @categories = Category.all
end end
def new
end
private private
def find_product def find_product
......
class StaticPagesController < ApplicationController class StaticPagesController < ApplicationController
def home
end
def help
end
def about
end
end end
...@@ -19,8 +19,4 @@ class Cart < ActiveRecord::Base ...@@ -19,8 +19,4 @@ class Cart < ActiveRecord::Base
def downcase_email def downcase_email
self.email = email.downcase self.email = email.downcase
end end
def check_valid
end
end end
...@@ -7,8 +7,4 @@ class CartProduct < ActiveRecord::Base ...@@ -7,8 +7,4 @@ class CartProduct < ActiveRecord::Base
validates :number, presence: true, format: { with: VALID_NUMBER_REGEX } validates :number, presence: true, format: { with: VALID_NUMBER_REGEX }
validates :price, presence: true, format: { with: VALID_NUMBER_REGEX } validates :price, presence: true, format: { with: VALID_NUMBER_REGEX }
def total_price
product.price * number
end
end end
...@@ -8,11 +8,8 @@ class User < ActiveRecord::Base ...@@ -8,11 +8,8 @@ class User < ActiveRecord::Base
:rememberable, :trackable, :validatable, :rememberable, :trackable, :validatable,
:authentication_keys => [:login] :authentication_keys => [:login]
VALID_PHONE_REGEX = /\d[0-9]\)*\z/
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :phone, length: { maximum: 12 },
format: { with: VALID_PHONE_REGEX }
validates :username, :presence => true, length: { maximum: 50 }, :uniqueness => { :case_sensitive => false } validates :username, :presence => true, length: { maximum: 50 }, :uniqueness => { :case_sensitive => false }
validates :email, presence: true, length: { maximum: 255 }, validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }, format: { with: VALID_EMAIL_REGEX },
......
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