- <%= link_to("Clear Cart",carts_path) %>
-
+ <%= link_to("Clear Cart",clean_carts_path, method: :delete, data: { confirm: "你确定要清空整个购物车吗?"}) %>
diff --git a/config/routes.rb b/config/routes.rb
index b59de1b6ae..7203cdf983 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -13,7 +13,12 @@
end
end
- resources :carts
+ resources :carts do
+ collection do
+ delete :clean
+ end
+ end
+
resources :cart_items
root 'products#index'
From bfc81bdc540429c7b5520e6c123694eab16b5e82 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 14:54:34 +0800
Subject: [PATCH 25/40] add_to_cart only one time
---
app/controllers/products_controller.rb | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb
index 7c35a32cf9..82b178905e 100644
--- a/app/controllers/products_controller.rb
+++ b/app/controllers/products_controller.rb
@@ -9,8 +9,15 @@ def show
def add_to_cart
@product = Product.find(params[:id])
- current_cart.add_product_to_cart(@product)
- flash[:notice] = "成功加入购物车"
+
+ if !current_cart.products.include?(@product)
+ # !current_cart.product_to_cart(@product) 我尝试的写法
+ current_cart.add_product_to_cart(@product)
+ flash[:notice] = "成功加入购物车!"
+ else
+ flash[:warning] = "你的购物车内已有此物品!"
+
+ end
redirect_to :back
end
From f2e83114be7b941b7cf0aa63f7949397b64396dd Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 15:32:30 +0800
Subject: [PATCH 26/40] add cart_item quantity in cart
---
app/controllers/cart_items_controller.rb | 14 ++++++++++++++
app/views/carts/index.html.erb | 8 +++++++-
2 files changed, 21 insertions(+), 1 deletion(-)
diff --git a/app/controllers/cart_items_controller.rb b/app/controllers/cart_items_controller.rb
index 951311b02b..269414aff2 100644
--- a/app/controllers/cart_items_controller.rb
+++ b/app/controllers/cart_items_controller.rb
@@ -10,4 +10,18 @@ def destroy
flash[:warning] = "成功将 #{@product.title} 从购物车删除!"
redirect_to :back
end
+
+ def update
+ @cart = current_cart
+ @cart_item = @cart.cart_items.find_by(product_id: params[:id])
+ @cart_item.update(cart_item_params)
+
+ redirect_to carts_path
+ end
+
+ private
+
+ def cart_item_params
+ params.require(:cart_item).permit(:quantity)
+ end
end
diff --git a/app/views/carts/index.html.erb b/app/views/carts/index.html.erb
index e2a7d1f2a9..9f14580b9e 100644
--- a/app/views/carts/index.html.erb
+++ b/app/views/carts/index.html.erb
@@ -36,7 +36,13 @@
<%= cart_item.product.price %>
|
- <%= cart_item.quantity %>
+ <% cart_item = current_cart.cart_items.find_by(product_id: cart_item.product_id) %>
+
+ <%= form_for cart_item, url: cart_item_path(cart_item.product_id) do |f| %>
+ <%= f.select :quantity, [1,2,3,4,5] %>
+ <%= f.submit "更新", data: { disable_with: "Submiting..." } %>
+ <% end %>
+
|
<%= link_to("Delete",cart_item_path(cart_item.product_id), :method => :delete) %>
From e6e1bf591b1b99e67cc7b1a28a02d86b103da712 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 15:51:29 +0800
Subject: [PATCH 27/40] add can not buy 0 quantity product
---
app/controllers/cart_items_controller.rb | 9 ++++++++-
app/views/products/show.html.erb | 6 +++++-
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/app/controllers/cart_items_controller.rb b/app/controllers/cart_items_controller.rb
index 269414aff2..a9450d9bcb 100644
--- a/app/controllers/cart_items_controller.rb
+++ b/app/controllers/cart_items_controller.rb
@@ -14,7 +14,14 @@ def destroy
def update
@cart = current_cart
@cart_item = @cart.cart_items.find_by(product_id: params[:id])
- @cart_item.update(cart_item_params)
+
+ if @cart_item.product.quantity >= cart_item_params[:quantity].to_i
+ @cart_item.update(cart_item_params)
+ flash[:notice] = "成功变更数量"
+ else
+ flash[:warning] = "数量不足以加入购物车"
+ end
+
redirect_to carts_path
end
diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb
index e26aea6e12..c5f9914798 100644
--- a/app/views/products/show.html.erb
+++ b/app/views/products/show.html.erb
@@ -16,7 +16,11 @@
数量 : <%= @product.quantity %>
¥ <%= @product.price %>
- <%= link_to("加入购物车", add_to_cart_product_path(@product), :method => :post, :class => "btn btn-primary btn-lg btn-danger") %>
+ <% if @product.quantity.present? && @product.quantity > 0 %>
+ <%= link_to("加入购物车", add_to_cart_product_path(@product), method: :post, class: "btn btn-lg btn-danger") %>
+ <% else %>
+ 已销售一空,无法购买
+ <% end %>
From aa88187880e2729f07e70ad7b2a83d5cdd80f5d7 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 15:58:42 +0800
Subject: [PATCH 28/40] add quantity of cart_item equal the prodcut quantity
---
app/views/carts/index.html.erb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/views/carts/index.html.erb b/app/views/carts/index.html.erb
index 9f14580b9e..89c509769c 100644
--- a/app/views/carts/index.html.erb
+++ b/app/views/carts/index.html.erb
@@ -39,7 +39,7 @@
<% cart_item = current_cart.cart_items.find_by(product_id: cart_item.product_id) %>
<%= form_for cart_item, url: cart_item_path(cart_item.product_id) do |f| %>
- <%= f.select :quantity, [1,2,3,4,5] %>
+ <%= f.select :quantity, 1..cart_item.product.quantity %>
<%= f.submit "更新", data: { disable_with: "Submiting..." } %>
<% end %>
From 540fa32c0ebd13e883bd90d6c694856bbba1dd40 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 16:23:56 +0800
Subject: [PATCH 29/40] add checkout routing
---
app/views/carts/index.html.erb | 2 +-
config/routes.rb | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/app/views/carts/index.html.erb b/app/views/carts/index.html.erb
index 89c509769c..20f3775b32 100644
--- a/app/views/carts/index.html.erb
+++ b/app/views/carts/index.html.erb
@@ -65,7 +65,7 @@
- <%= link_to("确认结账", "#", method: :post, class: "btn btn-lg btn-danger pull-right") %>
+ <%= link_to("确认结账", checkout_carts_path, method: :post, class: "btn btn-lg btn-danger pull-right") %>
diff --git a/config/routes.rb b/config/routes.rb
index 7203cdf983..606d0415b9 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -16,6 +16,7 @@
resources :carts do
collection do
delete :clean
+ post :checkout
end
end
From 137443bd058a89819d223b14371aaf4eb29bfbba Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 16:31:05 +0800
Subject: [PATCH 30/40] add order model
---
app/models/order.rb | 2 ++
db/migrate/20170208082750_create_orders.rb | 13 +++++++++++++
db/schema.rb | 13 ++++++++++++-
test/fixtures/orders.yml | 11 +++++++++++
test/models/order_test.rb | 7 +++++++
5 files changed, 45 insertions(+), 1 deletion(-)
create mode 100644 app/models/order.rb
create mode 100644 db/migrate/20170208082750_create_orders.rb
create mode 100644 test/fixtures/orders.yml
create mode 100644 test/models/order_test.rb
diff --git a/app/models/order.rb b/app/models/order.rb
new file mode 100644
index 0000000000..10281b3450
--- /dev/null
+++ b/app/models/order.rb
@@ -0,0 +1,2 @@
+class Order < ApplicationRecord
+end
diff --git a/db/migrate/20170208082750_create_orders.rb b/db/migrate/20170208082750_create_orders.rb
new file mode 100644
index 0000000000..627726eec4
--- /dev/null
+++ b/db/migrate/20170208082750_create_orders.rb
@@ -0,0 +1,13 @@
+class CreateOrders < ActiveRecord::Migration[5.0]
+ def change
+ create_table :orders do |t|
+ t.integer :total, default: 0
+ t.integer :user_id
+ t.string :billing_name
+ t.string :billing_address
+ t.string :shipping_name
+ t.string :shipping_address
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index d5e2175b82..14c0d17851 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170207075034) do
+ActiveRecord::Schema.define(version: 20170208082750) do
create_table "cart_items", force: :cascade do |t|
t.integer "cart_id"
@@ -25,6 +25,17 @@
t.datetime "updated_at", null: false
end
+ create_table "orders", force: :cascade do |t|
+ t.integer "total", default: 0
+ t.integer "user_id"
+ t.string "billing_name"
+ t.string "billing_address"
+ t.string "shipping_name"
+ t.string "shipping_address"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
create_table "products", force: :cascade do |t|
t.string "title"
t.text "description"
diff --git a/test/fixtures/orders.yml b/test/fixtures/orders.yml
new file mode 100644
index 0000000000..80aed36e30
--- /dev/null
+++ b/test/fixtures/orders.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
diff --git a/test/models/order_test.rb b/test/models/order_test.rb
new file mode 100644
index 0000000000..15b8ed1348
--- /dev/null
+++ b/test/models/order_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class OrderTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
From 0c59973fc94538b79e8a3af117e8874bf5826a0d Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 16:54:27 +0800
Subject: [PATCH 31/40] checkout page views
---
app/controllers/carts_controller.rb | 6 ++-
app/views/carts/checkout.html.erb | 75 +++++++++++++++++++++++++++++
config/routes.rb | 1 +
3 files changed, 81 insertions(+), 1 deletion(-)
create mode 100644 app/views/carts/checkout.html.erb
diff --git a/app/controllers/carts_controller.rb b/app/controllers/carts_controller.rb
index 96ad0dd919..756c6c8ea6 100644
--- a/app/controllers/carts_controller.rb
+++ b/app/controllers/carts_controller.rb
@@ -4,5 +4,9 @@ def clean
current_cart.clean!
flash[:warning] = "已清空购物车"
redirect_to carts_path
- end
+ end
+
+ def checkout
+ @order = Order.new
+ end
end
diff --git a/app/views/carts/checkout.html.erb b/app/views/carts/checkout.html.erb
new file mode 100644
index 0000000000..f249bf2a1a
--- /dev/null
+++ b/app/views/carts/checkout.html.erb
@@ -0,0 +1,75 @@
+
+
+
+ 购物明细
+
+
+
+
+ | 商品明细 |
+ 单价 |
+ 数量 |
+
+
+
+
+ <% current_cart.cart_items.each do |cart_item| %>
+
+ |
+ <%= link_to(cart_item.product.title, product_path(cart_item.product)) %>
+ |
+
+ <%= cart_item.product.price %>
+ |
+
+
+ <%= cart_item.quantity %>
+ |
+
+
+ <% end %>
+
+
+
+
+
+
+ 总计 <%= render_cart_total_price(current_cart) %> CNY
+
+
+
+
+
+ 订单资讯
+
+
+
+
diff --git a/config/routes.rb b/config/routes.rb
index 606d0415b9..7c28f929a3 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -21,6 +21,7 @@
end
resources :cart_items
+ resources :orders
root 'products#index'
#root 'admin/products#index'
From dc93813656a231dc656111501fa01080cf61718b Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 17:30:16 +0800
Subject: [PATCH 32/40] order controller
---
app/assets/javascripts/orders.coffee | 3 +++
app/assets/stylesheets/orders.scss | 3 +++
app/controllers/orders_controller.rb | 21 +++++++++++++++++++++
app/helpers/orders_helper.rb | 2 ++
app/models/order.rb | 7 +++++++
app/models/user.rb | 2 ++
test/controllers/orders_controller_test.rb | 7 +++++++
7 files changed, 45 insertions(+)
create mode 100644 app/assets/javascripts/orders.coffee
create mode 100644 app/assets/stylesheets/orders.scss
create mode 100644 app/controllers/orders_controller.rb
create mode 100644 app/helpers/orders_helper.rb
create mode 100644 test/controllers/orders_controller_test.rb
diff --git a/app/assets/javascripts/orders.coffee b/app/assets/javascripts/orders.coffee
new file mode 100644
index 0000000000..24f83d18bb
--- /dev/null
+++ b/app/assets/javascripts/orders.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
diff --git a/app/assets/stylesheets/orders.scss b/app/assets/stylesheets/orders.scss
new file mode 100644
index 0000000000..3b0428a94e
--- /dev/null
+++ b/app/assets/stylesheets/orders.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the orders controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb
new file mode 100644
index 0000000000..be524b631b
--- /dev/null
+++ b/app/controllers/orders_controller.rb
@@ -0,0 +1,21 @@
+class OrdersController < ApplicationController
+ before_action :authenticate_user!, only: [:create]
+
+ def create
+ @order = Order.new(order_params)
+ @order.user = current_user
+ @order.total = current_cart.total_price
+
+ if @order.save
+ redirect_to order_path(@order)
+ else
+ render 'carts/checkout'
+ end
+ end
+
+ private
+
+ def order_params
+ params.require(:order).permit(:billing_name, :billing_address, :shipping_name, :shipping_address)
+ end
+end
diff --git a/app/helpers/orders_helper.rb b/app/helpers/orders_helper.rb
new file mode 100644
index 0000000000..443227fd48
--- /dev/null
+++ b/app/helpers/orders_helper.rb
@@ -0,0 +1,2 @@
+module OrdersHelper
+end
diff --git a/app/models/order.rb b/app/models/order.rb
index 10281b3450..3f2946060b 100644
--- a/app/models/order.rb
+++ b/app/models/order.rb
@@ -1,2 +1,9 @@
class Order < ApplicationRecord
+ belongs_to :user
+
+ validates :billing_name, presence: true
+ validates :billing_address, presence: true
+ validates :shipping_name, presence: true
+ validates :shipping_address, presence: true
+
end
diff --git a/app/models/user.rb b/app/models/user.rb
index a8feefca87..3a99e8252c 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -8,4 +8,6 @@ def admin?
is_admin
end
# email == 'lilei@sina.com'
+
+ has_many :orders
end
diff --git a/test/controllers/orders_controller_test.rb b/test/controllers/orders_controller_test.rb
new file mode 100644
index 0000000000..859039113c
--- /dev/null
+++ b/test/controllers/orders_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class OrdersControllerTest < ActionDispatch::IntegrationTest
+ # test "the truth" do
+ # assert true
+ # end
+end
From 52383c42c8208795eb3db668c6855e324f6fb4ba Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 18:00:17 +0800
Subject: [PATCH 33/40] add product_list model
---
app/models/order.rb | 1 +
app/models/product_list.rb | 3 +++
db/migrate/20170208095515_create_product_lists.rb | 11 +++++++++++
db/schema.rb | 11 ++++++++++-
test/fixtures/product_lists.yml | 11 +++++++++++
test/models/product_list_test.rb | 7 +++++++
6 files changed, 43 insertions(+), 1 deletion(-)
create mode 100644 app/models/product_list.rb
create mode 100644 db/migrate/20170208095515_create_product_lists.rb
create mode 100644 test/fixtures/product_lists.yml
create mode 100644 test/models/product_list_test.rb
diff --git a/app/models/order.rb b/app/models/order.rb
index 3f2946060b..882de02a20 100644
--- a/app/models/order.rb
+++ b/app/models/order.rb
@@ -6,4 +6,5 @@ class Order < ApplicationRecord
validates :shipping_name, presence: true
validates :shipping_address, presence: true
+ has_many :product_lists
end
diff --git a/app/models/product_list.rb b/app/models/product_list.rb
new file mode 100644
index 0000000000..0eef1fc9dd
--- /dev/null
+++ b/app/models/product_list.rb
@@ -0,0 +1,3 @@
+class ProductList < ApplicationRecord
+ belongs_to :order
+end
diff --git a/db/migrate/20170208095515_create_product_lists.rb b/db/migrate/20170208095515_create_product_lists.rb
new file mode 100644
index 0000000000..14d4b78df6
--- /dev/null
+++ b/db/migrate/20170208095515_create_product_lists.rb
@@ -0,0 +1,11 @@
+class CreateProductLists < ActiveRecord::Migration[5.0]
+ def change
+ create_table :product_lists do |t|
+ t.integer :order_id
+ t.string :product_name
+ t.integer :product_price
+ t.integer :quantity
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 14c0d17851..e5c1a47d6d 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170208082750) do
+ActiveRecord::Schema.define(version: 20170208095515) do
create_table "cart_items", force: :cascade do |t|
t.integer "cart_id"
@@ -36,6 +36,15 @@
t.datetime "updated_at", null: false
end
+ create_table "product_lists", force: :cascade do |t|
+ t.integer "order_id"
+ t.string "product_name"
+ t.integer "product_price"
+ t.integer "quantity"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
create_table "products", force: :cascade do |t|
t.string "title"
t.text "description"
diff --git a/test/fixtures/product_lists.yml b/test/fixtures/product_lists.yml
new file mode 100644
index 0000000000..80aed36e30
--- /dev/null
+++ b/test/fixtures/product_lists.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+# This model initially had no columns defined. If you add columns to the
+# model remove the '{}' from the fixture names and add the columns immediately
+# below each fixture, per the syntax in the comments below
+#
+one: {}
+# column: value
+#
+two: {}
+# column: value
diff --git a/test/models/product_list_test.rb b/test/models/product_list_test.rb
new file mode 100644
index 0000000000..d5aace8f43
--- /dev/null
+++ b/test/models/product_list_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ProductListTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
From 56954086ae688f569a92bb3cd9cf25a3bed25567 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 18:09:13 +0800
Subject: [PATCH 34/40] order show
---
app/controllers/orders_controller.rb | 17 +++++++-
app/views/orders/show.html.erb | 64 ++++++++++++++++++++++++++++
2 files changed, 80 insertions(+), 1 deletion(-)
create mode 100644 app/views/orders/show.html.erb
diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb
index be524b631b..32c72d8458 100644
--- a/app/controllers/orders_controller.rb
+++ b/app/controllers/orders_controller.rb
@@ -6,13 +6,28 @@ def create
@order.user = current_user
@order.total = current_cart.total_price
- if @order.save
+ if @order.save
+ current_cart.cart_items.each do |cart_item|
+ product_list = ProductList.new
+ product_list.order = @order
+ product_list.product_name = cart_item.product.title
+ product_list.product_price = cart_item.product.price
+ product_list.quantity = cart_item.quantity
+ product_list.save
+ end
+
redirect_to order_path(@order)
else
render 'carts/checkout'
end
end
+ def show
+ @order = Order.find(params[:id])
+ @product_lists = @order.product_lists
+ end
+
+
private
def order_params
diff --git a/app/views/orders/show.html.erb b/app/views/orders/show.html.erb
new file mode 100644
index 0000000000..ba21a7329f
--- /dev/null
+++ b/app/views/orders/show.html.erb
@@ -0,0 +1,64 @@
+
+
+
+ 订单明细
+
+
+
+
+ | 商品明细 |
+ 单价 |
+
+
+
+
+ <% @product_lists.each do |product_list| %>
+
+ |
+ <%= product_list.product_name %>
+ |
+
+ <%= product_list.product_price %>
+ |
+
+ <% end %>
+
+
+
+
+
+
+ 总计 <%= @order.total %> CNY
+
+
+
+
+
+ 寄送资讯
+
+
+
+
+ |
+ 订购人
+ |
+
+
+ |
+ <%= @order.billing_name %> - <%= @order.billing_address %>
+ |
+
+
+ |
+ 收件人
+ |
+
+
+ |
+ <%= @order.shipping_name %> - <%= @order.shipping_address %>
+ |
+
+
+
+
+
From 8b7277c7d81fb3f8e7cd1bbedf97eee35a8f9469 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Wed, 8 Feb 2017 18:34:42 +0800
Subject: [PATCH 35/40] add_token_to_order
---
app/controllers/orders_controller.rb | 4 ++--
app/models/order.rb | 6 ++++++
db/migrate/20170208101417_add_token_to_order.rb | 5 +++++
db/schema.rb | 3 ++-
4 files changed, 15 insertions(+), 3 deletions(-)
create mode 100644 db/migrate/20170208101417_add_token_to_order.rb
diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb
index 32c72d8458..8dbc0a3db7 100644
--- a/app/controllers/orders_controller.rb
+++ b/app/controllers/orders_controller.rb
@@ -16,14 +16,14 @@ def create
product_list.save
end
- redirect_to order_path(@order)
+ redirect_to order_path(@order.token)
else
render 'carts/checkout'
end
end
def show
- @order = Order.find(params[:id])
+ @order = Order.find_by_token(params[:id])
@product_lists = @order.product_lists
end
diff --git a/app/models/order.rb b/app/models/order.rb
index 882de02a20..87693c9368 100644
--- a/app/models/order.rb
+++ b/app/models/order.rb
@@ -7,4 +7,10 @@ class Order < ApplicationRecord
validates :shipping_address, presence: true
has_many :product_lists
+
+ before_create :generate_token
+
+ def generate_token
+ self.token = SecureRandom.uuid
+ end
end
diff --git a/db/migrate/20170208101417_add_token_to_order.rb b/db/migrate/20170208101417_add_token_to_order.rb
new file mode 100644
index 0000000000..b427007c25
--- /dev/null
+++ b/db/migrate/20170208101417_add_token_to_order.rb
@@ -0,0 +1,5 @@
+class AddTokenToOrder < ActiveRecord::Migration[5.0]
+ def change
+ add_column :orders, :token, :string
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index e5c1a47d6d..6db557cdf3 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170208095515) do
+ActiveRecord::Schema.define(version: 20170208101417) do
create_table "cart_items", force: :cascade do |t|
t.integer "cart_id"
@@ -34,6 +34,7 @@
t.string "shipping_address"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
+ t.string "token"
end
create_table "product_lists", force: :cascade do |t|
From c293d7d5bdb6d4fcb7e886c33e43862709b698c7 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Thu, 9 Feb 2017 15:50:55 +0800
Subject: [PATCH 36/40] my orders in nav
---
app/assets/javascripts/account/orders.coffee | 3 +++
app/assets/stylesheets/account/orders.scss | 3 +++
app/controllers/account/orders_controller.rb | 6 +++++
app/helpers/account/orders_helper.rb | 2 ++
app/views/account/orders/index.html.erb | 24 +++++++++++++++++++
app/views/common/_navbar.html.erb | 1 +
config/routes.rb | 4 ++++
.../account/orders_controller_test.rb | 7 ++++++
8 files changed, 50 insertions(+)
create mode 100644 app/assets/javascripts/account/orders.coffee
create mode 100644 app/assets/stylesheets/account/orders.scss
create mode 100644 app/controllers/account/orders_controller.rb
create mode 100644 app/helpers/account/orders_helper.rb
create mode 100644 app/views/account/orders/index.html.erb
create mode 100644 test/controllers/account/orders_controller_test.rb
diff --git a/app/assets/javascripts/account/orders.coffee b/app/assets/javascripts/account/orders.coffee
new file mode 100644
index 0000000000..24f83d18bb
--- /dev/null
+++ b/app/assets/javascripts/account/orders.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://coffeescript.org/
diff --git a/app/assets/stylesheets/account/orders.scss b/app/assets/stylesheets/account/orders.scss
new file mode 100644
index 0000000000..53b61bacf9
--- /dev/null
+++ b/app/assets/stylesheets/account/orders.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the account/orders controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
diff --git a/app/controllers/account/orders_controller.rb b/app/controllers/account/orders_controller.rb
new file mode 100644
index 0000000000..ddec115e4a
--- /dev/null
+++ b/app/controllers/account/orders_controller.rb
@@ -0,0 +1,6 @@
+class Account::OrdersController < ApplicationController
+ before_action :authenticate_user!
+ def index
+ @orders = current_user.orders
+ end
+end
diff --git a/app/helpers/account/orders_helper.rb b/app/helpers/account/orders_helper.rb
new file mode 100644
index 0000000000..6f690c4e84
--- /dev/null
+++ b/app/helpers/account/orders_helper.rb
@@ -0,0 +1,2 @@
+module Account::OrdersHelper
+end
diff --git a/app/views/account/orders/index.html.erb b/app/views/account/orders/index.html.erb
new file mode 100644
index 0000000000..61219a7d7b
--- /dev/null
+++ b/app/views/account/orders/index.html.erb
@@ -0,0 +1,24 @@
+
+
+ 我的全部订单
+ 订单列表
+
+
+
+
+ | # |
+ 生成时间 |
+
+
+
+
+
+ <% @orders.each do |order| %>
+
+ | <%= link_to(order.id, order_path(order.token)) %> |
+ <%= order.created_at.to_s(:long) %> |
+
+ <% end %>
+
+
+
diff --git a/app/views/common/_navbar.html.erb b/app/views/common/_navbar.html.erb
index d876e62042..ad6db0f71e 100644
--- a/app/views/common/_navbar.html.erb
+++ b/app/views/common/_navbar.html.erb
@@ -34,6 +34,7 @@
<% if current_user.admin? %>
<%= link_to("Admin Panel", admin_products_path) %>
<% end %>
+ <%= link_to("My orders", account_orders_path) %>
<%= link_to("登出", destroy_user_session_path, method: :delete) %>
diff --git a/config/routes.rb b/config/routes.rb
index 7c28f929a3..69a29e2a43 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -23,6 +23,10 @@
resources :cart_items
resources :orders
+ namespace :account do
+ resources :orders
+ end
+
root 'products#index'
#root 'admin/products#index'
end
diff --git a/test/controllers/account/orders_controller_test.rb b/test/controllers/account/orders_controller_test.rb
new file mode 100644
index 0000000000..0addba5790
--- /dev/null
+++ b/test/controllers/account/orders_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class Account::OrdersControllerTest < ActionDispatch::IntegrationTest
+ # test "the truth" do
+ # assert true
+ # end
+end
From 9a37e94fd4238d5091d38c9704ed35e07f469e1f Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Thu, 9 Feb 2017 15:52:35 +0800
Subject: [PATCH 37/40] my order DESC
---
app/controllers/account/orders_controller.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/controllers/account/orders_controller.rb b/app/controllers/account/orders_controller.rb
index ddec115e4a..6fba4da7a0 100644
--- a/app/controllers/account/orders_controller.rb
+++ b/app/controllers/account/orders_controller.rb
@@ -1,6 +1,6 @@
class Account::OrdersController < ApplicationController
before_action :authenticate_user!
def index
- @orders = current_user.orders
+ @orders = current_user.orders.("id DESC")
end
end
From cc7e551aaee9a192e094e7231aa4d82b0b3c3050 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Thu, 9 Feb 2017 15:54:14 +0800
Subject: [PATCH 38/40] my order DESC R01
---
app/controllers/account/orders_controller.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/controllers/account/orders_controller.rb b/app/controllers/account/orders_controller.rb
index 6fba4da7a0..27860bc67e 100644
--- a/app/controllers/account/orders_controller.rb
+++ b/app/controllers/account/orders_controller.rb
@@ -1,6 +1,6 @@
class Account::OrdersController < ApplicationController
before_action :authenticate_user!
def index
- @orders = current_user.orders.("id DESC")
+ @orders = current_user.orders.order("id DESC")
end
end
From 83a4f784fc597126470e339637d518f6923a5304 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Mon, 13 Feb 2017 14:53:21 +0800
Subject: [PATCH 39/40] add commit
---
app/models/user.rb | 1 +
1 file changed, 1 insertion(+)
diff --git a/app/models/user.rb b/app/models/user.rb
index 3a99e8252c..e1e280f3ca 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -7,6 +7,7 @@ class User < ApplicationRecord
def admin?
is_admin
end
+# is_admin 是个users里的table,呼叫admin?输出boolean值
# email == 'lilei@sina.com'
has_many :orders
From 03035c79b5098e8a67a1aa45f76f766ded19bf79 Mon Sep 17 00:00:00 2001
From: kai <328458211@qq.com>
Date: Tue, 14 Feb 2017 17:42:22 +0800
Subject: [PATCH 40/40] add admin seed
---
db/seeds.rb | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/db/seeds.rb b/db/seeds.rb
index 1beea2accd..e6c215f6b8 100644
--- a/db/seeds.rb
+++ b/db/seeds.rb
@@ -5,3 +5,10 @@
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
+
+u = User.new
+u.email = "lilei@sina.com"
+u.password = "123456"
+u.password_confirmation = "123456"
+u.is_admin = true
+u.save
|