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> 數量 : <%= render_product_quantity(@product) %> </div> | |
<div class="product-price"> $ <%= render_product_price(@product) %> </div> | |
<div class="pull-right"> | |
<%= link_to("加入購物車", add_to_cart_product_path(@product) , :method => :post , :class => "btn btn-primary btn-lg btn-danger") %> | |
</div> | |
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
resources :products do | |
member do | |
post :add_to_cart | |
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
def add_to_cart | |
@product = Product.find(params[:id]) | |
if !current_cart.items.include?(@product) | |
current_cart.add_product_to_cart(@product) | |
flash[:notice] = "你已成功將 #{@product.title} 加入購物車" | |
else | |
flash[:warning] = "你的購物車內已有此物品" | |
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
helper_method :current_cart | |
def current_cart | |
@current_cart ||= find_cart | |
end | |
def find_cart | |
cart = Cart.find_by(id: session[:cart_id]) |
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 Cart < ActiveRecord::Base | |
has_many :cart_items, :dependent => :destroy | |
has_many :items, :through => :cart_items, :source => :product | |
def add_product_to_cart(product) | |
items << product | |
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 CartItem < ActiveRecord::Base | |
belongs_to :cart | |
belongs_to :product | |
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>商品資訊</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
<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
class CartsController < ApplicationController | |
before_action :authenticate_user!, :only => [:checkout] | |
def index | |
end | |
def checkout | |
@order = current_user.orders.build | |
@info = @order.build_info |
OlderNewer