在Ruby on Rails中添加购物车商品

8
我正在开发一个非常基础的购物车,遇到的问题是,如果我将同一产品添加到购物车中超过一次,我没有看到数量增加,而是看到多个版本的该商品。
例如,我看到:
1 x 绿色灯 = 15英镑 1 x 绿色灯 = 15英镑
而不是看到:
2 x 绿色灯 = 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
你如何添加这些项目?请展示代码。 - BvuRVKyUVlViVIc7
嗨@Lichtamberg,我已经添加了我用来将产品添加到购物车的链接 - 我只需通过post方法传递product_id。如果您能提供任何进一步的帮助,那就太好了!谢谢 :) - Tom Pinchen
1个回答

10

在您的Cart模型中,创建一个名为

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

这应该让你明白你想要实现什么。


嗨@scarver2,我在尝试将我的商品添加到购物篮时似乎遇到了“未初始化的常量CartController”错误。 我已经在标题为“新设置”的所有文件中进行了更新。 你能看出我的问题在哪里吗? 感谢你的帮助! - Tom Pinchen
如果您有第二个问题,应该在SO上发布一个新的问题。不要把多个问题放在一个问题中。 - Chris Peters
感谢您分享更多的代码。由于您的控制器和路由是复数形式,因此您需要将路由中的 to: 部分变为复数形式。post '/add_to_cart/:product_id' => 'carts#add_to_cart', :as => 'add_to_cart' - scarver2
@scarver2,我好像还有最后一个错误:CartsController#add_to_cart 中出现了 ActiveRecord::AssociationTypeMismatchItem(#53915280) expected, got Product(#53895696),你知道出了什么问题吗?谢谢! - Tom Pinchen
我编辑了我的回答。将“购物车中不存在该产品”部分更改为上述新代码。 - scarver2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接