2012-10-29 14 views
7

Ich entwickle einen sehr einfachen Einkaufswagen und das Problem, das ich habe, ist, dass, wenn ich mehr als ein Produkt in den Warenkorb legen, sehe ich keine Mengenzunahme und bin stattdessen nur mehrere Versionen des Artikels sehen.Warenkorb Artikel hinzufügen Ruby on Rails

Zum Beispiel sehe ich:

1 x grünes Licht = 15 £ 1 x grünes Licht = £ 15

eher als zu sehen:

2 x grünes Licht = 30 £

Wie kann ich es so einrichten, dass mein Warenkorb nach mehreren Versionen im Warenkorb sucht und diese dann zusammenfügt?

Zur Zeit habe ich:

Application Controller

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 

Wagen-Controller

def show 
    @cart = current_cart 
    end 

Wagen Modell

has_many :items 

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

Warenkorb Anzeigen

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

WEITERE INFORMATIONEN

Produkte # Index

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

Jede Beratung Menschen würde bieten können sehr geschätzt. Vielen Dank!

Neue Setup- - verursachende Fehler 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 

Carts-Controller

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 

Carts Modell

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 

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

Wie fügen Sie die Artikel hinzu? show code pls – Lichtamberg

+0

Hi @Lichtamberg, ich habe den Link hinzugefügt, den ich benutze, um das Produkt zu meinem Warenkorb hinzuzufügen - ich gebe einfach die Produkt-ID durch eine Post-Methode. Jede weitere Hilfe, die Sie anbieten können, wäre großartig! Danke :) –

Antwort

9

In Ihrem Warenkorb-Modell, ein Verfahren

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 

In routes.rb,

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

ändern In den Warenkorb Route zu einem Aufruf add_to_cart genannt erstellen Methode im Warenkorb-Controller.

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

Dies sollte Ihnen die Idee von dem, was Sie erreichen möchten, geben.

+0

sieht gut aus ...... – Lichtamberg

+0

Hi @ scarver2, ich scheine einen 'nicht initialisierten konstanten CartController' Fehler zu treffen, wenn ich versuche, meinen Artikel in einen Korb zu legen. Ich habe alle meine Dateien oben unter dem Titel "New Setup" aktualisiert. Kannst du sehen, wo ich falsch liege? Danke für Ihre Hilfe! –

+0

Wenn Sie eine zweite Frage haben, sollten Sie sie als neue Frage zu SO veröffentlichen. Packe nicht mehr als eine Frage in eine Frage ein. –