I’m working on building the shopping cart in the Depot tutorial in the
Agile Web D. book. I’m at the part (about page 80) where you
build the ability to add items to the cart. I’m able to get to
/store/display_cart/ and it seems items are being added, but if I just
click on a link to add an item (/store/add_to_cart/2) it gives me this:
NoMethodError in Store#add_to_cart
#{RAILS_ROOT}/app/models/cart.rb:10:in add_product' #{RAILS_ROOT}/app/controllers/store_controller.rb:11:in
add_to_cart’
Any ideas? Here are various bits of code:
===============================
#store_controller.rb
class StoreController < ApplicationController
def index
@products = Product.salable_items
end
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
redirect_to(:action => ‘display_cart’)
end
def display_cart
@cart = find_cart
@items = @cart.items
end
private
def find_cart
session[:cart] ||= Cart.new
end
end
===============================
#cart.rb
class Cart
attr_reader :items
attr_reader :total_price
def initialize
@items = []
@total_price = 0.0
end
def add_product(product)
@items << LineItem.for_product(product)
@total_price += product.price
end
end
===============================
#line_item.rb
class LineItem < ActiveRecord::Base
belongs_to :product
def self.for_product(product)
item = self.new
item.quantity = 1
item.product = product
item.unit_price = product.price
item
end
end
===============================
Anybody got any ideas?