2012-10-29 66 views
7

Tôi đang phát triển một giỏ mua hàng cơ bản và vấn đề tôi gặp phải là nếu tôi thêm nhiều sản phẩm vào giỏ hàng của mình, tôi không thấy số lượng tăng và thay vào đó chỉ nhìn thấy nhiều phiên bản của mục.Thêm giỏ hàng Ruby on Rails

Ví dụ tôi thấy:

1 x bật đèn xanh = £ 15 1 x màu xanh nhạt = £ 15

chứ không phải nhìn thấy:

2 x màu xanh nhạt = £ 30

Tôi làm cách nào để giỏ hàng của mình kiểm tra nhiều phiên bản trong giỏ hàng và sau đó thêm chúng lại với nhau?

Hiện nay tôi có:

điều khiển ứng dụng

def current_cart 
    if session[:cart_id] 
    @current_cart ||= Cart.find(session[:cart_id]) 
    end 
    if session[:cart_id].nil? 
    @current_cart = Cart.create! 
    session[:cart_id] = @current_cart.id 
    end 
    @current_cart 
    end 

giỏ khiển

def show 
    @cart = current_cart 
    end 

giỏ Mẫu

has_many :items 

def total_price 
    items.to_a.sum(&:full_price) 
end 

giỏ hàng Xem

<table id="line_items"> 
    <tr> 
    <th>Product</th> 
    <th>Qty</th> 
    <th class="price">Unit Price</th> 
    <th class="price">Full Price</th> 
    </tr> 

    <% for item in @cart.items %> 
    <tr class="<%= cycle :odd, :even %>"> 
     <td><%=h item.product.name %></td> 
     <td class="qty"><%= item.quantity %></td> 
     <td class="price"><%= gbp(item.unit_price) %></td> 
     <td class="price"><%= gbp(item.full_price) %></td> 
    </tr> 
    <% end %> 
<tr> 

    <td class="total price" colspan="4"> 
    Total: <%= gbp(@cart.total_price) %> 
    </td> 
    </tr> 
    </table> 

THÊM THÔNG TIN

Sản phẩm # Index

<%= link_to "Add to Cart", line_items_path(:product_id => product), :method => :post %> 

Bất cứ lời khuyên mọi người có thể cung cấp sẽ được nhiều đánh giá cao. Cảm ơn!

mới cài đặt - Gây Lỗi Uninitialised Constant CartController

routes.rb

Checkout::Application.routes.draw do 

    ActiveAdmin.routes(self) 

    devise_for :admin_users, ActiveAdmin::Devise.config 

    post '/add_to_cart/:product_id' => 'cart#add_to_cart', :as => 'add_to_cart' 

    resources :carts 
    resources :products 
    resources :items 

    root :to => 'products#index' 

end 

giỏ khiển

class CartsController < ApplicationController 

    def show 
    @cart = current_cart 
    end 

    def add_to_cart 
     current_cart.add_item(params[:product_id]) 
     redirect_to carts_path(current_cart.id) 
    end 

end 

giỏ hàng mẫu

class Cart < ActiveRecord::Base 
has_many :items 

def add_item(product_id) 
     item = items.where('product_id = ?', product_id).first 
    if item 
     # increase the quantity of product in cart 
     item.quantity + 1 
     save 
    else 
     # product does not exist in cart 
     product = Product.find(product_id) 
     items << product 
    end 
    save 
end 

def total_price 
    items.to_a.sum(&:full_price) 
end 
end 

Product # Index

<table class="jobs"> 
    <thead> 
     <tr> 
      <th scope="col" id="name">Product Code</th> 
      <th scope="col" id="company">Name</th> 
      <th scope="col" id="company">Price</th> 
      <th scope="col" id="company">Action</th> 
     </tr> 
    </thead> 

    <tbody> 
     <% @product.each do |product| %> 
     <tr>  
      <td><%= product.product_code %></td> 
      <td><%= product.name %></td> 
      <td><%= gbp(product.price) %></td> 
      <td><%= button_to "Add to Cart", add_to_cart_path(:product_id => product), :method => :post %></td> 
     </tr> 
     <% end %> 
    </tbody> 
</table> 
+3

cách bạn thêm các mục? show code pls – Lichtamberg

+0

Xin chào @Lichtamberg, tôi đã thêm liên kết tôi sử dụng để thêm sản phẩm vào Giỏ hàng của mình - Tôi chỉ cần chuyển product_id qua phương thức đăng bài. Mọi trợ giúp thêm bạn có thể cung cấp sẽ tuyệt vời! cảm ơn :) –

Trả lời

9

Ở mô hình giỏ hàng của bạn, tạo ra một phương pháp gọi là

def add_item(product_id) 
    item = items.where('product_id = ?', product_id).first 
    if item 
    # increase the quantity of product in cart 
    item.quantity + 1 
    save 
    else 
    # product does not exist in cart 
    cart.items << Item.new(product_id: product_id, quantity: 1) 
    end 
    save 
end 

Ở routes.rb,

post '/add_to_cart/:product_id' => 'cart#add_to_cart', :as => 'add_to_cart' 

Thay đổi Add bạn vào Giỏ đường đến một cuộc gọi add_to_cart trong bộ điều khiển Cart.

def add_to_cart 
    current_cart.add_item(params[:product_id]) 
    # redirect to shopping cart or whereever 
end 

Điều này sẽ cho bạn ý tưởng về những gì bạn muốn thực hiện.

+0

có vẻ tốt ...... – Lichtamberg

+0

Hi @ scarver2, tôi dường như đang gặp lỗi 'Uninitialised constant CartController' khi cố gắng thêm mục của tôi vào giỏ. Tôi đã cập nhật tất cả các tệp của mình ở trên dưới tiêu đề 'Thiết lập mới'. Bạn có thể thấy nơi tôi đang đi sai? Cảm ơn bạn đã giúp đỡ! –

+0

Nếu bạn có câu hỏi thứ 2, bạn nên đăng câu hỏi đó dưới dạng câu hỏi mới trên SO. Đừng đóng gói nhiều câu hỏi vào một câu hỏi. –