Commit 79799b81 by Nguyen Quoc Kien

Fix code admin: add products

parent d895a13f
......@@ -26,11 +26,7 @@ class Admin::ProductsController < ApplicationController
end
def create
@product = Product.new(name: params[:product][:name],
category_id: params[:product][:category_id].to_i,
price: params[:product][:price].to_i,
image: params[:product][:image],
description: params[:product][:description])
@product = Product.new(product_params)
if @product.check_valid()
@product.save
flash[:success] = "Create product : Success"
......
......@@ -5,13 +5,14 @@ class Admin < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
before_save :downcase_email
validates :username, :presence => true, length: { maximum: 50 }, :uniqueness => { :case_sensitive => false }
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
before_save :downcase_email
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
......
......@@ -4,7 +4,6 @@ class Product < ActiveRecord::Base
belongs_to :category
has_many :cart_products
before_destroy :ensure_not_referenced_by_any_cart_product
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
......@@ -13,6 +12,9 @@ class Product < ActiveRecord::Base
validates :description, presence: true, length: { maximum: 65535 }
validates :price, presence: true, format: { with: VALID_NUMBER_REGEX }
before_destroy :ensure_not_referenced_by_any_cart_product
before_save :convert_data_product
def check_valid
if self.price < 0
return false
......@@ -23,6 +25,7 @@ class Product < ActiveRecord::Base
if self.image == nil
return false
end
return true
end
def self.search(keyword)
......@@ -39,4 +42,9 @@ class Product < ActiveRecord::Base
return false
end
end
def convert_data_product
self.category_id.to_i
self.price.to_i
end
end
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