NoMethodError in StoreController#add_to_cart

I’m trying to set up the Depot app from Agile Web D. and I
got this error after C1 - Creating a Cart.

The exact error looks like this:


NoMethodError in StoreController#add_to_cart

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.<<

E:/InstantRails/rails_apps/depot/app/models/cart.rb:9:in add_product' E:/InstantRails/rails_apps/depot/app/controllers/store_controller.rb: 8:inadd_to_cart’

The contents of cart.rb:

class Cart
attr_reader :items

def initialize
@items = []
end

def add_product(product)
@itmes << product
debugger
end
end

The contents of store_controller.rb

class StoreController < ApplicationController
def index
@products = Product.find_products_for_sale
end
def add_to_cart
@cart = find_cart
product = Product.find(params[:id])
@cart.add_product(product)
debugger
end

private

def find_cart
session[:cart] ||=Cart.new
end
end

What could be the problem?

On Apr 15, 3:06 pm, korre [email protected] wrote:

    def add_product(product)
            @itmes << product
            debugger

You’ve typoed @items

Fred

Yup. That did the trick :slight_smile: Ooops…

On Apr 15, 5:15 pm, Frederick C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs