更新库存数量根据在rails应用程序中的购物车数量?

问题描述:

我正在开发电子商务应用程序,我无法弄清楚在用户将产品添加到购物车时如何更新产品库存数量。 因为我是一个总新手,所以我跟着这个应用程序的大部分教程。但是,教程不会介绍如何在用户将产品添加到购物车时更新产品库存数量。更新库存数量根据在rails应用程序中的购物车数量?

到目前为止,我已经添加了此方法将cart.rb模型

def upd_quantity 
    if cart.purchased_at 
    product.decrement!(quantity: params[:stock_quantity]) 
    end 
end 

但I'm很坚持,不知道我在做什么 这将是巨大的,如果有人可以看看在这里,并告诉我如何实现这个功能的应用程序。

这里是对GitHub库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 

是否加入这一行: product.update_columns(stock_quantity: product.stock_quantity - 1)

要在CartsController中添加动作,请执行此操作吗?

​​

试试这个从购物车删除产品:

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'以保持一致性。

+0

Ohhhh呀!非常感谢你的工作,但在某种程度上。如果我在购物车中添加产品,库存数量会下降,但是如果我从购物车中删除产品,库存数量不会再增加 – DaudiHell

+1

请参阅编辑,删除操作的建议 – matthewalexander

+0

这是一个很好的建议,但它在CartsController#remove'错误中给出了一个'NoMethodError。说'未定义的方法'stock_quantity'为{“product_id”=>“14”,“数量”=> 1}:哈希'在这一行'item = item.update_columns(stock_quantity:item.stock_quantity + 1)' – DaudiHell