i am trying to build a shop, but i have an error after removing my items from cart, while try to add the cart.items.quantity back to the product.stock_quantity. This are the error messages:
NoMethodError in CartsController#remove
undefined method `quantity' for {"product_id"=>"1", "quantity"=>21}:Hash
.
Routing Error
No route matches [GET] "/cart/destroy/1"
Here is the important part of my 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
If i change the item.quantity to just +1 , everything works fine. But i actually want to add the hole cart.item.quantity back to the stock_quantity. Here is the routes file:
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
Does anyone have an idea?
As the error indicates, item
is a hash
. So, you can't call item.quantity
. Try item[:quantity]
instead.
BTW, it's generally considered (IMO) bad form to post images of error messages. It may be difficult for folks to view an image on various devices (e.g., phones). Also, with an image, we are unable to copy and paste relevant code to help answer your question. It is better if you copy and paste the error into your question.