2012-10-29 15 views
7

Pracuję nad bardzo prostym koszykiem, a problem, który mam, polega na tym, że jeśli dodaję więcej niż jeden produkt do koszyka, nie widzę zwiększenia ilości i zamiast tego jestem po prostu widząc wiele wersji przedmiotu.Dodawanie pozycji koszyka Ruby on Rails

Przykładowo widać:

1 x zielone światło = 15 £ 1 x zielone światło = 15 £

zamiast zobaczyć:

2 x zielone światło = £ 30

Jak mogę to zrobić, aby mój koszyk sprawdził wiele wersji w koszyku, a następnie dodał je razem?

Obecnie mam:

Controller Application

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 

Koszyk Controller

def show 
    @cart = current_cart 
    end 

Koszyk model

has_many :items 

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

Koszyk Zobacz

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

INNE INFORMACJE

Produkty Wskaźnik

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

Wszelkie porady ludzie mogą oferować będzie bardzo cenione. Dzięki!

Nowa konfiguracja - Spowodowanie Błąd 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 

Wózki 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 

Wózki model

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 

wyrobów Wskaźnik

<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

Jak dodać przedmioty? pokaż kod pls – Lichtamberg

+0

Witam @Lichtamberg, dodałem link, którego używam, aby dodać produkt do mojego koszyka - Po prostu przekazuję product_id przez metodę post. Dalsza pomoc, jaką możesz zaoferować, byłaby świetna! dzięki :) –

Odpowiedz

9

W modelu wózka, utworzyć metodę zwaną

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 

W routes.rb,

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

Zmień dodaj do koszyka drogę do add_to_cart połączeń metoda w sterowniku koszyka.

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

To powinno dać ci wyobrażenie o tym, co chcesz osiągnąć.

+0

wygląda dobrze ...... – Lichtamberg

+0

Witam @ scarver2, wydaje mi się, że uderzam w błąd "niezainicjowany stały błąd CartController'a" podczas próby dodania mojego przedmiotu do koszyka. Zaktualizowałem wszystkie moje pliki powyżej pod tytułem 'Nowa konfiguracja'. Czy widzisz, gdzie idę źle? Dzięki za pomoc! –

+0

Jeśli masz drugie pytanie, powinieneś opublikować je jako nowe pytanie na SO. Nie należy pakować więcej niż jednego pytania na pytanie. –