2012-10-29 196 views
7

我正在开发一个非常基本的购物车,我遇到的问题是,如果我在购物车中添加多个相同的产品,我看不到数量增加,而只是看到该项目的多个版本。添加购物车项目Ruby on Rails

例如我看到:

1×绿光=£15 1×绿光=£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

你如何添加项目?显示代码请致电 – Lichtamberg

+0

Hi @Lichtamberg您好@Lichtamberg,我添加了用于将产品添加到我的购物车中的链接 - 我只需通过post方法传递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方法在Cart控制器中。

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

这应该让你知道你想要完成什么。

+0

看起来不错...... – Lichtamberg

+0

嗨@ scarver2,我试图将我的物品添加到购物篮时似乎遇到了'未初始化的常量CartController'错误。我已经在标题'新建安装'下更新了上述所有文件。你能看到我要去哪里吗?谢谢你的帮助! –

+0

如果你有第二个问题,你应该把它作为一个新的问题发布在SO上。不要在问题中包含多个问题。 –

相关问题