This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateOrders < ActiveRecord::Migration | |
| def change | |
| create_table :orders do |t| | |
| t.integer :user_id | |
| t.integer :total | |
| t.boolean :paid, :default => false | |
| t.timestamps | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateOrderInfos < ActiveRecord::Migration | |
| def change | |
| create_table :order_infos do |t| | |
| t.integer :order_id | |
| t.string :billing_name | |
| t.string :billing_address | |
| t.string :shipping_name | |
| t.string :shipping_address | |
| t.timestamps |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class CreateOrderItems < ActiveRecord::Migration | |
| def change | |
| create_table :order_items do |t| | |
| t.string :product_name | |
| t.float :price | |
| t.integer :quantity | |
| t.integer :order_id | |
| t.timestamps | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class OrdersController < ApplicationController | |
| before_action :authenticate_user! | |
| def create | |
| @order = current_user.orders.build(order_params) | |
| if @order.save | |
| @order.build_item_cache_from_cart(current_cart) | |
| @order.calculate_total!(current_cart) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Order < ActiveRecord::Base | |
| belongs_to :user | |
| has_many :items, :class_name => "OrderItem", :dependent => :destroy | |
| has_one :info, :class_name => "OrderInfo", :dependent => :destroy | |
| accepts_nested_attributes_for :info | |
| def build_item_cache_from_cart(cart) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class OrderInfo < ActiveRecord::Base | |
| belongs_to :order | |
| validates :billing_name , :presence => true | |
| validates :billing_address , :presence => true | |
| validates :shipping_name , :presence => true | |
| validates :shipping_address , :presence => true | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class="row"> | |
| <div class="col-md-12"> | |
| <h2> 訂單明細 </h2> | |
| <table class="table table-bordered"> | |
| <thead> | |
| <tr> | |
| <th width="80%">商品明細</th> | |
| <th>單價</th> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def show | |
| @order = current_user.orders.find_by_token(params[:id]) | |
| @order_info = @order.info | |
| @order_items = @order.items | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <link rel="stylesheet" type="text/css" href="/assets/application.css" /> | |
| <div class="row"> | |
| <div class="col-md-12"> | |
| <h2> 訂單明細 </h2> | |
| <table class="table table-borderd"> | |
| <thead> | |
| <tr> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class OrderMailer < ActionMailer::Base | |
| default from: "[email protected]" | |
| def notify_order_placed(order) | |
| @order = order | |
| @user = order.user | |
| @order_items = @order.items | |
| @order_info = @order.info |