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
...@@ -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
...@@ -57,4 +62,12 @@ class CartsController < ApplicationController ...@@ -57,4 +62,12 @@ class CartsController < ApplicationController
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
begin
category = Category.find(params[:id]) category = Category.find(params[:id])
@products = category.products @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
begin
@products = Product.paginate(page: params[:page]) @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,6 +2,7 @@ ...@@ -2,6 +2,7 @@
<div class="span9"> <div class="span9">
<h2>Shopping Cart</h2> <h2>Shopping Cart</h2>
<% if !session[:cart].nil? && !session[:cart].empty? %>
<div> <div>
<ul> <ul>
<table class="table table-striped table-hover"> <table class="table table-striped table-hover">
...@@ -49,8 +50,11 @@ ...@@ -49,8 +50,11 @@
<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 "Continue Shopping " , root_path, class: "btn btn-primary" %>
<%= link_to "Check Out", "/cart", class: "btn btn-success pull-right"%>&nbsp;&nbsp; <%= 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 "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 "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,16 +32,27 @@ ...@@ -32,16 +32,27 @@
<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'%>
<% else %>
<%= f.email_field :email, class: 'form-control'%>
<% end %>
<%= f.label :fullname %> <%= f.label :fullname %>
<%= f.text_field :fullname, class: 'form-control' %> <%= f.text_field :fullname, class: 'form-control' %>
<%= f.label :address %> <%= f.label :address %>
......
<%= 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,9 +3,9 @@ class ImportAmazon ...@@ -3,9 +3,9 @@ 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
......
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