STEP 1
1. 加入app/views/products/show.html.erb
<%= link_to("加入購物車", add_to_cart_product_path(@product), :method => :post, :class => "btn btn-primary btn-lg btn-danger") =>
2. 加入app/controller/products_controller.rb
def add_to_cart
@product = Product.find(params[:id])
redirect_to :back
flash[:notice] = "測試加入購物車"
end
3. 修改config/routes.rb
root 'product#index'
devise_for :users
namespace :admin do
resources :products
end
-resources :products
+ resources :products do
+ member do
+ post :add_to_cart
+ end
+end
end
git add .
git commit -m "add add_to_cart button"
STEP 2
1. 產(chǎn)生cart與cartitem兩個model
rails g model cart
rails g model cart_item
2. 修改cartitem的migration
+t.integer :cart_id
+t.integer :product_id
+t.integer :quantity, default: 1
rake db:migrate
3. 修改app/models/cart.rb
+has_many :cart_items
+has_many :products, through: :cart_items ,source::product
4. 修改app/models/cart_item.rb
+belongs_to :cart
+belongs_to :product
5. 實作add_product_to_cart
app/models/cart.rb
+def add_product_to_cart(product)
+ci = cart_items.build
+ci.product = product
+ci.quantity = 1
+ci.save
+end
git add .
git commit -m "implement add_product_to_cart function"
STEP 3
1. 顯示購物車內(nèi)物品數(shù)量
修改app/views/common/_navbar.html.erb
<li>
<%= link_to "#" do %>
購物車 <i class="fa fa-shopping-cart"> </i> ( <%= current_cart.product.count %>
<% end %>
</li>
2. 加入購物車代碼
修改app/controllers/application_controller.rb
...略
+ helper_method :current_cart
+ def current_cart
+ @current_cart ||= find_cart
+ end
+ private
+ def find_cart
+ cart = Cart.find_by(id: session[:cart_id])
if cart.blank?
+ cart = Cart.create
+ end
+ session[:cart_id] = cart.id
+ return cart
+ end
3. 加入current_cart.add_product_to_cart(@product)
修改app/controllers/products_controller.rb
def add_to_cart
@product = Product.find(params[:id])
- redirect_to :back
- flash[:notice] = "測試加入購物車"
+ current_cart.add_product_to_cart(@product)
+ flash[:notice] = "成功加入購物車"
+ redirect_to :back
end
git add .
git commit -m "implement current_cart function"
STEP 4顯示購物車明細
1. 新增carts controller
rails g controller carts
2. 修改config/routes.rb
+resources :carts
修改app/views/common/_navbar.html.erb
<li>
- <%= link_to "#" do %>
+ <%= link_to carts_path do %>
購物車 <i class="fa fa-shopping-cart"> </i> ( <%= current_cart.products.count %> )
<% end %>
</li>
3. 實作購物明細
touch app/views/carts/index.html.erb
<div class="row">
<div class="col-md-12">
<h2> 購物車 </h2>
<table class="table table-bordered">
<thead>
<tr>
<th colspan="2">商品資訊</th>
<th>單價</th>
<th>數(shù)量</th>
</tr>
</thead>
<tbody>
<% current_cart.cart_items.each do |cart_item| %>
<tr>
<td>
<%= link_to product_path(cart_item.product) do %>
<% if cart_item.product.image.present? %>
<%= image_tag(cart_item.product.image.thumb.url, class: "thumbnail") %>
<% else %>
<%= image_tag("http://placehold.it/200x200&text=No Pic", class: "thumbnail") %>
<% end %>
<% end %>
</td>
<td>
<%= link_to(cart_item.product.title, product_path(cart_item.product)) %>
</td>
<td>
<%= cart_item.product.price %>
</td>
<td>
<%= cart_item.quantity %>
</td>
</tr>
<% end %>
</tbody>
</table>
<br>
<div class="total clearfix">
<span class="pull-right">
<span> 總計 xxx RMB </span>
</span>
</div>
<hr>
<div class="checkout clearfix">
<%= link_to("確認結(jié)賬", "#", method: :post, class: "btn btn-lg btn-danger pull-right") %>
</div>
</div>
</div>
git add .
git commit -m "implement carts index function"
STEP5 計算總價
1. 修改app/views/carts/index.html.erb
...(略)
<div class="total clearfix">
<span class="pull-right">
<span>
- 總計 xxx RMB
+ <% sum = 0 %>
+ <% current_cart.cart_items.each do |cart_item| %>
+ <% if cart_item.product.price.present? %>
+ <% sum = sum + cart_item.quantity * cart_item.product.price %>
+ <% end %>
+ <% end %>
+ 總計 <%= sum %> RMB
</span>
</span>
</div>
...(略)
2. Refactor到Helper去
修改app/views/carts/index.html.erb
...(略)
<div class="total clearfix">
<span class="pull-right">
<span>
- <% sum = 0 %>
- <% current_cart.cart_items.each do |cart_item| %>
- <% if cart_item.product.price.present? %>
- <% sum = sum + cart_item.quantity * cart_item.product.price %>
- <% end %>
- <% end %>
- 總計 <%= sum %> RMB
+ 總計 <%= render_cart_total_price(current_cart) %> RMB
</span>
</span>
</div>
...(略)
修改app/helpers/carts_helper.rb
module CartsHelper
+ def render_cart_total_price(cart)
+ sum = 0
+ cart.cart_items.each do |cart_item|
+ if cart_item.product.price.present?
+ sum += cart_item.quantity * cart_item.product.price
+ end
+ end
+ sum
+ end
end
3.Refactor到Model去(如何做的更好)
修改app/helpers/cart_helper.rb
module CartsHelper
def render_cart_total_price(cart)
- sum = 0
- cart.cart_items.each do |cart_item|
- if cart_item.product.price.present?
- sum += cart_item.quantity * cart_item.product.price
- end
- end
- sum
+ cart.total_price
end
end
修改app/model/cart.rb
class Cart < ApplicationRecord
# ... 略
+ def total_price
+ sum = 0
+ cart_items.each do |cart_item|
+ if cart_item.product.price.present?
+ sum += cart_item.quantity * cart_item.product.price
+ end
+ end
+ sum
+ end
end
git add .
git commit -m "implement cart_total_price function"