2012-10-29 14 views
7

मैं एक बहुत ही बुनियादी शॉपिंग कार्ट विकसित कर रहा हूं और मेरा मुद्दा यह है कि यदि मैं अपने शॉपिंग कार्ट में एक से अधिक उत्पाद जोड़ता हूं तो मुझे मात्रा में वृद्धि नहीं दिखाई दे रही है और इसके बजाय आइटम के कई संस्करण देख रहे हैं।कार्ट आइटमों को जोड़ना रेल पर रूबी

उदाहरण के लिए मैं देख रहा हूँ:

1 x हरी बत्ती = £ 15 1 x हरी बत्ती = £ 15

बजाय देखकर:

2 एक्स हरी बत्ती = £ 30

मैं इसे कैसे बना सकता हूं ताकि मेरा शॉपिंग कार्ट कार्ट में कई संस्करणों की जांच करेगा और फिर उन्हें एक साथ जोड़ देगा?

वर्तमान में मेरे पास है:

आवेदन नियंत्रक

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 

गाड़ी नियंत्रक

def show 
    @cart = current_cart 
    end 

कार्ट मॉडल

has_many :items 

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

कार्ट देखें

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

अतिरिक्त जानकारी

उत्पाद # सूचकांक

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

किसी भी सलाह लोगों पेशकश कर सकते हैं बहुत सराहना की जाएगी। धन्यवाद!

नए सेटअप - कारण त्रुटि 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 

गाड़ियां नियंत्रक

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 

गाड़ियां मॉडल

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 

उत्पाद # सूचकांक

<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

आप आइटम कैसे जोड़ते हैं? कोड pls – Lichtamberg

+0

हाय @ लिचमबर्ग, मैंने अपने कार्ट में उत्पाद जोड़ने के लिए उपयोग किया गया लिंक जोड़ा है - मैं बस एक पोस्ट विधि के माध्यम से product_id पास करता हूं। आप जो भी मदद कर सकते हैं वह बहुत अच्छा होगा! धन्यवाद :) –

उत्तर

9

अपने कार्ट मॉडल में, एक विधि

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' 

बदलें अपनी एक फोन add_to_cart को गाड़ी मार्ग में जोड़ें कार्ट नियंत्रक में विधि।

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

इससे आपको यह पता होना चाहिए कि आप क्या हासिल करना चाहते हैं।

+0

अच्छा लग रहा है ...... – Lichtamberg

+0

हाय @ स्कार्वर 2, मुझे लगता है कि मेरी वस्तु टोकरी में जोड़ने की कोशिश करते समय 'अनियंत्रित निरंतर कार्टकंट्रोलर' त्रुटि को मार रहा है। मैंने 'नई सेटअप' शीर्षक के तहत उपरोक्त सभी फाइलें अपडेट की हैं। क्या आप देख सकते हैं कि मैं कहां गलत हो रहा हूं? आपकी सहायताके लिए धन्यवाद! –

+0

यदि आपके पास दूसरा प्रश्न है, तो आपको इसे SO पर एक नए प्रश्न के रूप में पोस्ट करना चाहिए। एक प्रश्न में एक से अधिक प्रश्न पैक न करें। –

संबंधित मुद्दे