NoMethodError in StoreController#add_to_cart -> getting nil

Hey everybody,

I am currently following the depot application from the book Web Agile
Development with Rails (3rd Edition) but after creating the controller
for the Store and the Cart model, I am always getting a nil product, no
matter how many products I add to the database.
I am very new with RoR (this only the page 110 of the book) and thus I
am not very sure about what I could do to mitigate the problem.

Thanks a lot in advance!

Manuel Manuel wrote:

Hey everybody,

I am currently following the depot application from the book Web Agile
Development with Rails (3rd Edition) but after creating the controller
for the Store and the Cart model, I am always getting a nil product, no
matter how many products I add to the database.
I am very new with RoR (this only the page 110 of the book) and thus I
am not very sure about what I could do to mitigate the problem.

Thanks a lot in advance!

The errors are both Cart.rb, line number 9:

def add_product(product)
    -> @items << product
end

and

def add_to_cart
    product = Product.find(params[:id])
    @cart = find_cart
-> @cart.add_product(product)
end

Error:
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<