从购物车中移除商品并将此数量添加回 stock_quantity (rubyonrails)

Remove items from cart and add this quantity back to the stock_quantity (rubyonrails)

我正在尝试建立一家商店, 但是从购物车中取出我的物品后出现错误, 同时尝试将 cart.items.quantity 添加回 product.stock_quantity。 这是错误消息:

NoMethodError in CartsController#remove
undefined method `quantity' for {"product_id"=>"1", "quantity"=>21}:Hash

.

Routing Error
No route matches [GET] "/cart/destroy/1"

这是我的 carts_controller 的重要部分:

    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 + item.quantity)

    if item
        cart['items'].delete item
    end
  redirect_to cart_path
end

如果我将 item.quantity 更改为 +1 ,一切正常。 但我实际上想把 hole cart.item.quantity 添加回 stock_quantity。 这是路由文件:

Rails.application.routes.draw do
resources :stockists
post 'emaillist/subscribe' => 'emaillist#subscribe'
get 'products/search' => 'products#search', as: 'search_products'
get 'pages/index'
get 'pages/about'
get 'pages/help'
devise_for :users
resources :categories
resources :categories
resources :designers
resources :category_names
resources :products
resource :cart, only: [:show] do
post "add", path: "add/:id", on: :member
delete 'remove', path: 'destroy/:id'
get :checkout
end
resources :orders, only: [ :index, :show, :create, :update ] do
member do
get :new_payment
post :pay
end
end

有人有想法吗?

如错误所示,itemhash。所以,你不能调用 item.quantity。请尝试 item[:quantity]

顺便说一句,错误消息的 post 图像通常被认为 (IMO) 格式不正确。人们可能很难在各种设备(例如电话)上查看图像。此外,对于图像,我们无法复制和粘贴相关代码来帮助回答您的问题。最好将错误复制并粘贴到您的问题中。