Commit bd0447a0 by Nguyen Quoc Kien

Fix bug

parent 37430b6e
......@@ -52,8 +52,3 @@ group :development, :test do
gem 'spring'
end
group :production do
gem 'pg', '0.17.1'
gem 'rails_12factor', '0.0.2'
end
......@@ -94,7 +94,6 @@ GEM
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
orm_adapter (0.5.0)
pg (0.17.1)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
......@@ -117,11 +116,6 @@ GEM
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
rails_12factor (0.0.2)
rails_serve_static_assets
rails_stdout_logging
rails_serve_static_assets (0.0.4)
rails_stdout_logging (0.0.3)
railties (4.2.2)
actionpack (= 4.2.2)
activesupport (= 4.2.2)
......@@ -183,9 +177,7 @@ DEPENDENCIES
jbuilder (~> 2.0)
jquery-rails
mysql2
pg (= 0.17.1)
rails (= 4.2.2)
rails_12factor (= 0.0.2)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
spring
......
......@@ -3,7 +3,7 @@ class Admin::ProductsController < ApplicationController
before_action :authenticate_admin!
def index
@products = Product.paginate(page: params[:page]).per_page(21)
@products = Product.paginate(page: params[:page]).per_page(50)
@categories = Category.all
end
......@@ -27,28 +27,21 @@ class Admin::ProductsController < ApplicationController
def create
@product = Product.new(product_params)
if @product.check_valid()
@product.save
if @product.save
flash[:success] = "Create product : Success"
redirect_to admin_products_path
else
flash[:danger] = "Error"
flash[:danger] = "Error: New product"
redirect_to new_admin_product_path
end
end
def update
if params[:product][:price].to_i > 0
if@product.check_valid()
@product.update(product_params)
if @product.update(product_params)
flash[:success] = "Update product : Success"
redirect_to admin_products_path
else
flash[:danger] = "Error"
redirect_to edit_admin_product_path(id: params[:id])
end
else
flash[:danger] = "Error: Price"
flash[:danger] = "Error: Update product"
redirect_to edit_admin_product_path(id: params[:id])
end
end
......
......@@ -24,8 +24,8 @@ class CartsController < ApplicationController
flash[:success] = "Email to send"
redirect_to products_path
else
flash[:danger] = "Error"
redirect_to carts_path
flash[:danger] = "Error: Create carts"
redirect_to :back
end
end
......
......@@ -8,28 +8,17 @@ class Product < ActiveRecord::Base
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :category_id, presence: true
validates :name, presence: true
validates :image, presence: true, length: { maximum: 1000 }
validates :description, presence: true, length: { maximum: 65535 }
validates :price, presence: true, format: { with: VALID_NUMBER_REGEX }
validates_numericality_of :price, presence: true, format: { with: VALID_NUMBER_REGEX }, greater_than: 0
before_destroy :ensure_not_referenced_by_any_cart_product
before_save :convert_data_product
def check_valid
if self.price < 0
return false
end
if self.description == nil
return false
end
if self.image == nil
return false
end
return true
end
def self.search(keyword)
Product.where("name like '%#{keyword}%'")
Product.where("name like '%?%'", keyword)
end
private
......
......@@ -2,6 +2,7 @@
<h2 style="text-align: center;">Edit products <%= @product.id %></h2>
<div class="col-9">
<%= form_for [:admin, @product] do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: 'form-control' %><br/>
......
......@@ -2,6 +2,7 @@
<h2 style="text-align: center;">New products </h2>
<div class="col-9 ">
<%= form_for [:admin, @product] do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: 'form-control' %><br/>
......
<% if @product.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@product.errors.count, "error") %>.
</div>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
\ No newline at end of file
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