Daði Hall Daði Hall - 3 months ago 11
Ruby Question

Couldn't find Product with 'id'= when adding products to Cart in Rails app

When I try to add products to a Cart in a App that I'm building I always get this error

Couldn't find Product with 'id'=
. According to Better Errors this is happening in the first line on the Create method in my
product_items_controller.rb
as seen below.

def create
@product = Product.find(params[:product_id])
@product_item = @cart.add_product(product.id)
if @product_item.save
redirect_to root_url, notice:'Product added to Cart'
else
render :new
end
end


I changed the first line to :
@product = Product.find(params[:id])
but that didn't correct the error.

Earlier tonight I modified the
Add to Cart
button code
from :
<%= button_to product_items_path(product_id: product) do %>
to
<%= button_to product_items_path( @product) do %>


This is the code for the Add to Cart button as it is now.

<%= button_to product_items_path( @product) do %>
<i class="fa fa-shopping-cart"></i>Add to Cart
<% end %>


UPDATE, ADDED ROUTES.rb

here is the routes.rb

Rails.application.routes.draw do

resources :categories
resources :labels
resources :products

resources :carts
resources :product_items
resources :orders

devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
root 'pages#index'

end


ANOTHER EDIT

here is the
cart.rb
which holds the add_product method

class Cart < ActiveRecord::Base

has_many :product_items, dependent: :destroy

def add_product(product_id)
current_item = product_items.find_by(product_id: product_id)
if current_item
current_item.quantity += 1
else
current_item = product_items.build(product_id: product_id)
end
current_item
end

def total_price_usd
product_items.to_a.sum{|item| item.total_price_usd}
end

def total_price_isl
product_items.to_a.sum{|item| item.total_price_isl}
end
end


UPDATE

here is the link to the github repo https://github.com/DadiHall/hlinreykdal

I have created all product through Active Admin, and that works like it should work.
Am I missing something here?
I can't figure out why this error keeps coming.

Answer

The problem is with the following line of code.

@product = Product.find(params[:product_id])

params[:product_id]is nil cause its not present in the params hash. To know what params holds, you can do something like this.

def create
  render text: params
end

Now, if you try to create a new product_item, you will find the params passed to the create action.

However if you want product_id to be passed to the create action, you need nested routes.

resources :products do
  resources :product_items
end

And change your html as

<%= button_to product_product_items_path( @product) do %>
  <i class="fa fa-shopping-cart"></i>Add to Cart
<% end %>

You can find the routes generated by running rake routes.

In the second line of the creation action,

@product_item = @cart.add_product(product.id)

I am not sure what product is. Should it be

@product_item = @cart.add_product(@product.id)

?