我正在开发电子商务应用程序,当用户将产品添加到购物车时,我无法弄清楚如何更新产品库存数量。 因为我是一个新手,所以我遵循了大部分应用程序的教程。但是,当用户将产品添加到购物车时,本教程不会讨论如何更新产品库存数量。
到目前为止,我已将此方法添加到cart.rb
模型
def upd_quantity
if cart.purchased_at
product.decrement!(quantity: params[:stock_quantity])
end
end
但我真的被困住了,不知道我在做什么 如果有人可以看看这个并告诉我如何将这个功能应用到应用程序,那将是很棒的。
这是指向github repo https://github.com/DadiHall/brainstore
的链接这就是我现在所拥有的。
cart_item.rb
型号
class CartItem
attr_reader :product_id, :quantity
def initialize product_id, quantity = 1
@product_id = product_id
@quantity = quantity
end
def increment
@quantity = @quantity + 1
end
def product
Product.find product_id
end
def total_price
product.price * quantity
end
def upd_quantity
if cart.purchased_at
product.decrement!(quantity: params[:stock_quantity])
end
end
end
cart.rb
模型
class Cart
attr_reader :items
def self.build_from_hash hash
items = if hash ["cart"] then
hash["cart"] ["items"].map do |item_data|
CartItem.new item_data["product_id"], item_data["quantity"]
end
else
[]
end
new items
end
def initialize items = []
@items = items
end
def add_item product_id
item = @items.find { |item| item.product_id == product_id }
if item
item.increment
else
@items << CartItem.new(product_id)
end
end
def empty?
@items.empty?
end
def count
@items.length
end
def serialize
items = @items.map do |item|
{
"product_id" => item.product_id,
"quantity" => item.quantity
}
end
{
"items" => items
}
end
def total_price
@items.inject(0) { |sum, item| sum + item.total_price }
end
end
product.rb
型号
class Product < ActiveRecord::Base
mount_uploader :image, ImageUploader
validates_presence_of :name, :price, :stock_quantity
validates_numericality_of :price, :stock_quantity
belongs_to :designer
belongs_to :category
belongs_to :page
def self.search(query)
where("name LIKE ? OR description LIKE ?", "%#{query}%", "%#{query}%")
end
end
`cart_controller.rb`
class CartsController < ApplicationController
before_filter :initialize_cart
def add
@cart.add_item params[:id]
session["cart"] = @cart.serialize
product = Product.find params[:id]
redirect_to :back, notice: "Added #{product.name} to cart."
end
def show
end
def checkout
@order_form = OrderForm.new user: User.new
@client_token = Braintree::ClientToken.generate
end
def remove
cart = session['cart']
item = cart['items'].find { |item| item['product_id'] == params[:id] }
if item
cart['items'].delete item
end
redirect_to cart_path
end
end
答案 0 :(得分:1)
添加此行:
product.update_columns(stock_quantity: product.stock_quantity - 1)
要在CartsController中添加操作吗?
def add
@cart.add_item params[:id]
session["cart"] = @cart.serialize
product = Product.find params[:id]
product.update_columns(stock_quantity: product.quantity - 1)
redirect_to :back, notice: "Added #{product.name} to cart."
end
尝试将此产品从购物车中移除:
def remove
cart = session['cart']
item = cart['items'].find { |item| item['product_id'] == params[:id] }
product = Product.find(item['product_id'])
product.update_columns(stock_quantity: product.stock_quantity + 1)
if item
cart['items'].delete item
end
redirect_to cart_path
end
请注意,这仅适用于您一次添加和删除1个商品/产品的情况。我建议您在删除操作中将“item”重命名为“product”以保持一致性。