Commit f67de3d0 by Dao Minh Nhut

fix bug

parent 9452905f
class Admin::UsersController < ApplicationController class Admins::UsersController < ApplicationController
def show def show
@users = User.all @users = User.all
end end
end
end
\ No newline at end of file
...@@ -7,6 +7,7 @@ class CartsController < ApplicationController ...@@ -7,6 +7,7 @@ class CartsController < ApplicationController
end end
def info def info
@new_cart = Cart.new
if user_signed_in? if user_signed_in?
@current_user @current_user
end end
...@@ -14,17 +15,19 @@ class CartsController < ApplicationController ...@@ -14,17 +15,19 @@ class CartsController < ApplicationController
def create def create
if !session[:cart].nil? if !session[:cart].nil?
new_cart = Cart.new(cart_params) @new_cart = Cart.new(fullname: params[:cart][:fullname], email: params[:cart][:email],
new_cart.total_price = calculate_total_price address: params[:cart][:address], phone: params[:cart][:phone],
new_cart.status = "new cart" total_price: calculate_total_price, status: "new cart", user_id: cart_user_id)
if user_signed_in? #new_cart.total_price = calculate_total_price
new_cart.user_id = current_user.id #new_cart.status = "new cart"
end #if user_signed_in?
new_cart.save # new_cart.user_id = current_user.id
if !new_cart.id.nil? #end
if @new_cart.save
#if !new_cart.id.nil?
session[:cart].each do |id, quantity| session[:cart].each do |id, quantity|
cart_product = CartProduct.new cart_product = CartProduct.new
cart_product.cart_id = new_cart.id cart_product.cart_id = @new_cart.id
cart_product.product_id = id cart_product.product_id = id
cart_product.price = Product.find_by_id(id).price cart_product.price = Product.find_by_id(id).price
cart_product.quantity = quantity cart_product.quantity = quantity
...@@ -33,14 +36,16 @@ class CartsController < ApplicationController ...@@ -33,14 +36,16 @@ class CartsController < ApplicationController
flash[:success] = "Success!" flash[:success] = "Success!"
Emailer.send_email_to(cart_params[:email].to_s,session[:cart]).deliver Emailer.send_email_to(cart_params[:email].to_s,session[:cart]).deliver
session[:cart] = nil session[:cart] = nil
redirect_to cart_path
else
render :info
end end
end end
flash[:danger] = "Wrong input please input again!" flash[:danger] = "Wrong input please input again!"
render :info
end end
def cart_params def cart_params
params.require(:session).permit(:fullname, :email, :address, :phone) params.require(:cart).permit(:fullname, :email, :address, :phone)
end end
private private
...@@ -49,12 +54,20 @@ class CartsController < ApplicationController ...@@ -49,12 +54,20 @@ class CartsController < ApplicationController
total = 0 total = 0
if !session[:cart].nil? if !session[:cart].nil?
session[:cart].each do |id, quantity| session[:cart].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
end
end end
end end
end
total total
end end
def cart_user_id
user_id = ""
if user_signed_in?
user_id = current_user.id
end
user_id
end
end end
class CategoriesController < ApplicationController class CategoriesController < ApplicationController
include CategoriesHelper include CategoriesHelper
def show def show
category = Category.find(params[:id]) begin
@products = category.products category = Category.find(params[:id])
@products = category.products
rescue
redirect_to root_path
end
end end
end end
\ No newline at end of file
...@@ -2,7 +2,11 @@ class ProductsController < ApplicationController ...@@ -2,7 +2,11 @@ class ProductsController < ApplicationController
include CategoriesHelper include CategoriesHelper
include CartProductsHelper include CartProductsHelper
def index def index
@products = Product.paginate(page: params[:page]) begin
@products = Product.paginate(page: params[:page])
rescue
@products = Product.paginate(page: "1")
end
end end
def show def show
begin begin
......
...@@ -2,11 +2,10 @@ class Cart < ActiveRecord::Base ...@@ -2,11 +2,10 @@ class Cart < ActiveRecord::Base
has_many :cart_product has_many :cart_product
VALID_PHONE_REGEX = /\d[0-9]\)*\z/ 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
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX } validates :email, presence: true, length: { maximum: 100 }, format: { with: VALID_EMAIL_REGEX }
validates :phone, presence: true, length: { maximum: 15 }, format: { with: VALID_PHONE_REGEX } validates :phone, presence: true, length: { maximum: 15 }, format: { with: VALID_PHONE_REGEX }
validates :total_price, presence: true, format: { with: VALID_NUMBER_REGEX } validates :total_price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 99999999 }
validates :fullname, presence: true, length: { maximum: 50 } validates :fullname, presence: true, length: { maximum: 50 }
validates :address, presence: true, length: { maximum: 1000 } validates :address, presence: true, length: { maximum: 1000 }
end end
class Category < ActiveRecord::Base class Category < ActiveRecord::Base
has_many :products has_many :products
def new
@category = Category.new
end
end end
class Product < ActiveRecord::Base class Product < ActiveRecord::Base
belongs_to :category belongs_to :category
has_many :cart_products has_many :cart_products
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :category_id, presence: true validates :category_id, presence: true
validates :name, :image, presence: true, length: { maximum: 1000 } validates :name, :image, presence: true, length: { maximum: 1000 }
validates :description, length: { maximum: 65535 } validates :description, length: { maximum: 65535 }
validates :price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 9999 } validates :price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 99999999 }
validates :page, :numericality => { :greater_than_or_equal_to => 0 }
end end
<% if admin_signed_in? %> <% if admin_signed_in? %>
<div class="span12"> <div class="span12">
<h2>Products</h2> <h2>Products</h2>
<%= link_to "Insert Product", insert_product_path %> <%= link_to "Insert Product", insert_product_path, class: "pull-right"%>
<table class="table table-striped table-hover"> <table class="table table-striped table-hover">
<thead> <thead>
<tr> <tr>
<th>Product Name</th> <th>Product Name</th>
<th>Image</th>
<th>Edit</th> <th>Edit</th>
<th>Delete</th> <th>Delete</th>
</tr> </tr>
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
<% @products.each do |product| %> <% @products.each do |product| %>
<tr> <tr>
<td><%= product.name %></td> <td><%= product.name %></td>
<td><%= image_tag product.image, height: '60', width: '60' %></td>
<td><%= link_to "Edit", "/update_product/#{product.id}" %></td> <td><%= link_to "Edit", "/update_product/#{product.id}" %></td>
<td><%= link_to "Delete", "/delete_product/#{product.id}" %></td> <td><%= link_to "Delete", "/delete_product/#{product.id}" %></td>
</tr> </tr>
......
...@@ -2,55 +2,59 @@ ...@@ -2,55 +2,59 @@
<div class="span9"> <div class="span9">
<h2>Shopping Cart</h2> <h2>Shopping Cart</h2>
<div> <% if !session[:cart].nil? && !session[:cart].empty? %>
<ul> <div>
<table class="table table-striped table-hover"> <ul>
<thead> <table class="table table-striped table-hover">
<tr> <thead>
<th>Product Name</th> <tr>
<th>Quantity</th> <th>Product Name</th>
<th>Update</th> <th>Quantity</th>
<th>Delete</th> <th>Update</th>
<th>Price</th> <th>Delete</th>
<th>Total</th> <th>Price</th>
</tr> <th>Total</th>
</thead> </tr>
<tbody> </thead>
<% total = 0 %> <tbody>
<% if !@cart_product.nil? %> <% total = 0 %>
<% @cart_product.each do |id, quantity| %> <% if !@cart_product.nil? %>
<% product = Product.find_by_id(id) %> <% @cart_product.each do |id, quantity| %>
<% if !product.nil? %> <% product = Product.find_by_id(id) %>
<% total = total + product.price * quantity.to_i %> <% if !product.nil? %>
<tr> <% total = total + product.price * quantity.to_i %>
<td><%= link_to truncate(product.name, length:20), "/products/#{product.id}" %></td> <tr>
<form action="/cart_product/update" > <td><%= link_to truncate(product.name, length:20), "/products/#{product.id}" %></td>
<td><input name="new_quantity" min="1" max="100" type="number" class="span1" value= <%= quantity %> /></td> <form action="/cart_product/update" >
<td><input type="submit" value="Update" /><input type="hidden" name="id" value="<%= product.id %>"/></td> <td><input name="new_quantity" min="1" max="100" type="number" class="span1" value= <%= quantity %> /></td>
</form> <td><input type="submit" value="Update" /><input type="hidden" name="id" value="<%= product.id %>"/></td>
<td><form action="/cart_product/remove" ><input type="submit" value="Delete" /><input type="hidden" name="id" value="<%= product.id %>"/></form></td> </form>
<td><%= (product.price/100.to_f).to_s + "$" %></td> <td><form action="/cart_product/remove" ><input type="submit" value="Delete" /><input type="hidden" name="id" value="<%= product.id %>"/></form></td>
<td><%= ((product.price * quantity)/100.to_f).to_s + "$" %></td> <td><%= (product.price/100.to_f).to_s + "$" %></td>
</tr> <td><%= ((product.price * quantity)/100.to_f).to_s + "$" %></td>
</tr>
<% end %>
<% end %> <% end %>
<% else %>
<h2>Shopping Cart Empty</h2>
<% end %> <% end %>
<% else %> </tbody>
<h2>Shopping Cart Empty</h2> </table>
<% end %> </ul>
</tbody> </div>
</table>
</ul>
</div>
<dl class="dl-horizontal pull-right"> <dl class="dl-horizontal pull-right">
<dt>Sub-total:</dt> <dt>Sub-total:</dt>
<dd><%=(total/100.to_f).to_s + "$" %></dd> <dd><%=(total/100.to_f).to_s + "$" %></dd>
<dt>Total:</dt> <dt>Total:</dt>
<dd><%=(total/100.to_f).to_s + "$" %></dd> <dd><%=(total/100.to_f).to_s + "$" %></dd>
</dl> </dl>
<div class="clearfix"></div> <div class="clearfix"></div>
<%= link_to "Check Out", "/cart", class: "btn btn-success pull-right"%>&nbsp;&nbsp;
<%= link_to "Delete All" ,'/cart_product/clear', class: "btn btn-lg btn-danger pull-right" %>
<%else%>
<h1>Your Cart is Empty!</h1>
<% end %>
<%= link_to "Continue Shopping " , root_path, class: "btn btn-primary" %> <%= link_to "Continue Shopping " , root_path, class: "btn btn-primary" %>
<%= link_to "Check Out", "/cart", class: "btn btn-success pull-right"%>&nbsp;&nbsp;
<%= link_to "Delete All" ,'/cart_product/clear', class: "btn btn-lg btn-danger pull-right" %>
<!--%= link_to "info", "/carts", class: "btn btn-lg btn-success"%--> <!--%= link_to "info", "/carts", class: "btn btn-lg btn-success"%-->
</div> </div>
\ No newline at end of file
...@@ -32,25 +32,36 @@ ...@@ -32,25 +32,36 @@
<dd><%= number_to_currency(@total/100.to_f, :unit => '$')%></dd> <dd><%= number_to_currency(@total/100.to_f, :unit => '$')%></dd>
</dl></div><br><br><br> </dl></div><br><br><br>
<div align="center"> <div align="center">
<% flash.each do |message_type, message| %> <% if @new_cart.errors.any? %>
<div class="alert alert-<%= message_type %>"><%= message %></div> <div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@new_cart.errors.count, "error") %>.
</div>
<ul>
<% @new_cart.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %> <% end %>
<div> <div>
<%= form_for(@new_cart, url: create_cart_path) do |f| %>
<%= f.label :email %>
<% if user_signed_in? %> <% if user_signed_in? %>
<% email = @current_user.email %> <% email = @current_user.email %>
<% end %>
<%= form_for(:session, url: create_cart_path) do |f| %>
<%= f.label :email %>
<%= f.email_field :email, value: email, class: 'form-control'%> <%= f.email_field :email, value: email, class: 'form-control'%>
<%= f.label :fullname %> <% else %>
<%= f.text_field :fullname, class: 'form-control' %> <%= f.email_field :email, class: 'form-control'%>
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
</br>
<%= f.submit "Submit" , class: "btn btn-primary" %>
<% end %> <% end %>
<%= f.label :fullname %>
<%= f.text_field :fullname, class: 'form-control' %>
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
</br>
<%= f.submit "Submit" , class: "btn btn-primary" %>
<% end %>
</div> </div>
<%else%> <%else%>
<h1>Your Cart is Empty. Thank you for order</h1> <h1>Your Cart is Empty. Thank you for order</h1>
......
<%= render 'categories/view' %> <%= render 'categories/view' %>
<div class="span9"> <div class="span9">
<div class="hero-unit"> <div class="hero-unit">
<h1 class="">Ventura Trainee</h1> <h1 class="">Ventura Trainee</h1>
<p class="">Ruby on Rails</p> <p class="">Ruby on Rails</p>
......
VenShop::Application.routes.draw do VenShop::Application.routes.draw do
namespace :admins do namespace :admins do
get 'users/show'
end
namespace :admins do
get 'users/new' get 'users/new'
end end
......
...@@ -4,7 +4,7 @@ class CreateProducts < ActiveRecord::Migration ...@@ -4,7 +4,7 @@ class CreateProducts < ActiveRecord::Migration
t.string :name t.string :name
t.string :image t.string :image
t.integer :price t.integer :price
t.string :description, limit: 65535 t.string :description
t.integer :category_id t.integer :category_id
t.timestamps null: false t.timestamps null: false
......
class CreateCarts < ActiveRecord::Migration class CreateCarts < ActiveRecord::Migration
def change def change
create_table :carts do |t| create_table :carts do |t|
t.integer :User_id t.integer :user_id
t.integer :total_price t.integer :total_price
t.string :status t.string :status
t.string :fullname t.string :fullname
t.integer :phone t.string :phone
t.string :address t.string :address
t.string :email t.string :email
......
class ChangeLimit < ActiveRecord::Migration
def change
change_column :products, :price, :integer, :limit => 5
change_column :products, :description, :string, :limit => 20000
change_column :cart_products, :cart_id, :integer, :limit => 5
change_column :cart_products, :product_id, :integer, :limit => 5
change_column :cart_products, :quantity, :integer, :limit => 5
change_column :cart_products, :price, :integer, :limit => 5
change_column :carts, :user_id, :integer, :limit => 5
change_column :carts, :total_price, :integer, :limit => 5
change_column :products, :name, :string, :limit => 1000
change_column :products, :category_id, :integer, :limit => 5
end
end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150730075349) do ActiveRecord::Schema.define(version: 20150804082910) do
create_table "admins", force: :cascade do |t| create_table "admins", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false t.string "email", limit: 255, default: "", null: false
...@@ -32,20 +32,20 @@ ActiveRecord::Schema.define(version: 20150730075349) do ...@@ -32,20 +32,20 @@ ActiveRecord::Schema.define(version: 20150730075349) do
add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true, using: :btree add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true, using: :btree
create_table "cart_products", force: :cascade do |t| create_table "cart_products", force: :cascade do |t|
t.integer "cart_id", limit: 4 t.integer "cart_id", limit: 8
t.integer "product_id", limit: 4 t.integer "product_id", limit: 8
t.integer "quantity", limit: 4 t.integer "quantity", limit: 8
t.integer "price", limit: 4 t.integer "price", limit: 8
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
create_table "carts", force: :cascade do |t| create_table "carts", force: :cascade do |t|
t.integer "User_id", limit: 4 t.integer "user_id", limit: 8
t.integer "total_price", limit: 4 t.integer "total_price", limit: 8
t.string "status", limit: 255 t.string "status", limit: 255
t.string "fullname", limit: 255 t.string "fullname", limit: 255
t.integer "phone", limit: 4 t.string "phone", limit: 255
t.string "address", limit: 255 t.string "address", limit: 255
t.string "email", limit: 255 t.string "email", limit: 255
t.datetime "created_at", null: false t.datetime "created_at", null: false
...@@ -59,11 +59,11 @@ ActiveRecord::Schema.define(version: 20150730075349) do ...@@ -59,11 +59,11 @@ ActiveRecord::Schema.define(version: 20150730075349) do
end end
create_table "products", force: :cascade do |t| create_table "products", force: :cascade do |t|
t.string "name", limit: 255 t.string "name", limit: 1000
t.string "image", limit: 255 t.string "image", limit: 255
t.integer "price", limit: 4 t.integer "price", limit: 8
t.string "description", limit: 20000 t.string "description", limit: 20000
t.integer "category_id", limit: 4 t.integer "category_id", limit: 8
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end
......
...@@ -3,17 +3,17 @@ class ImportAmazon ...@@ -3,17 +3,17 @@ class ImportAmazon
def initialize def initialize
@request = Vacuum.new('US') @request = Vacuum.new('US')
@request.configure( @request.configure(
aws_access_key_id: "AKIAJ77C4CTZOP7TUVWQ", aws_access_key_id: 'AKIAIAJR65JO6EIPQWTA',
aws_secret_access_key: "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", aws_secret_access_key: '8rpb5q169RUtj7HU3njH3zxcKthZJmWbgtrzESXy',
associate_tag: "zigexn6400-22" associate_tag: 'microv'
) )
@request.associate_tag = 'tag' @request.associate_tag = 'tag'
end end
def import_product def import_product
(1..100).each do |page| (1..100).each do |page|
get_response(page).each do |item| get_response(page).each do |item|
begin begin
category = Category.find_or_create_by(name: item["ItemAttributes"]["ProductGroup"]) category = Category.find_or_create_by(name: item["ItemAttributes"]["ProductGroup"])
products = Product.find_or_create_by(name: item["ItemAttributes"].to_h["Title"]) do |products| products = Product.find_or_create_by(name: item["ItemAttributes"].to_h["Title"]) do |products|
products.image = item["LargeImage"]["URL"] products.image = item["LargeImage"]["URL"]
...@@ -41,7 +41,7 @@ class ImportAmazon ...@@ -41,7 +41,7 @@ class ImportAmazon
'ItemPage' => page, 'ItemPage' => page,
} }
) )
response.to_h["ItemSearchResponse"].to_h["Items"].to_h["Item"] response.to_h["ItemSearchResponse"].to_h["Items"].to_h["Item"]
end end
end end
\ No newline at end of file
require 'test_helper' require 'test_helper'
class Admins::UsersControllerTest < ActionController::TestCase class Admins::UsersControllerTest < ActionController::TestCase
test "should get show" do test "should get new" do
get :show get :new
assert_response :success assert_response :success
end end
......
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one: one:
User_id: 1 user_id: 1
total_price: 1 total_price: 1
status: MyString status: MyString
fullname: MyString fullname: MyString
phone: 1 phone: MyString
address: MyString address: MyString
email: MyString email: MyString
two: two:
User_id: 1 user_id: 1
total_price: 1 total_price: 1
status: MyString status: MyString
fullname: MyString fullname: MyString
phone: 1 phone: MyString
address: MyString address: MyString
email: MyString email: MyString
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