2012-10-29 13 views
7

Çok temel bir alışveriş sepeti geliştiriyorum ve şu anda sahip olduğum sorun şu ki, alışveriş sepetime aynı ürünü birden fazla eklersek miktar artışını görmüyorum. öğenin birden çok sürümünü görmek.Sepet Sepetleri Ekleme Ruby on Rails

Örneğin görüyorum:

1 x yeşil ışık = £ 15 1 x yeşil ışık = £ 15

ziyade görme:

2 x yeşil ışık = 30 £

Alışveriş sepetimin sepetteki birden çok sürümü kontrol edip sonra da eklemesi için nasıl yapabilirim?

Şu anda var:

Uygulama Kontrolörü

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 

Sepet Kontrolör

def show 
    @cart = current_cart 
    end 

Sepet Modeli

has_many :items 

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

Sepeti Görüntüle

<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> 

FAZLA BİLGİ

Ürünler # İndeksi

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

kişi sunabilir Herhangi bir tavsiye çok takdir edilecektir. Teşekkürler!

Yeni Kurulum - Neden Olan Hata 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 

Arabaları Kontrolörü

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 

Arabaları Modeli

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 

Ürün # Endeksinin Sepeti modelinde

<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

Öğeleri nasıl ekliyorsunuz? kodu göster – Lichtamberg

+0

Merhaba @Lichtamberg, Ürünümü sepete eklemek için kullandığım bağlantıyı ekledim - sadece bir post yöntemiyle product_id ürününü iletiyorum. Sunduğunuz başka herhangi bir yardım harika olurdu! teşekkürler :) –

cevap

9

, routes.rb olarak

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 

adlı bir yöntem oluşturmak,

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

bir çağrı ADD_TO_CART için sepeti rotayı Ekle 'değiştirin Sepeti denetleyicide yöntem.

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

Bu, neyi başarmak istediğinizi size bildirmelidir.

+0

iyi görünüyor ...... – Lichtamberg

+0

Merhaba @ scarver2, bir sepet benim öğe eklemek çalışırken bir 'uninitialised sabit CartController' hatasını vurmak gibi görünüyor. Yukarıdaki tüm dosyalarımı 'Yeni Kurulum' başlığı altında güncelledim. Nereye gittiğimi görebiliyor musun? Yardım ettiğin için teşekkür ederim! –

+0

Eğer ikinci bir sorunuz varsa, bunu SO üzerinde yeni bir soru olarak göndermelisiniz. Bir soruya birden fazla soru sormayın. –