Newbie error: undefined local variable or method

When I add an item to the shopping cart, I get an error:

undefined local variable or method `cart_item’ for
#<#Class:0x3715a78:0x3715a48>

Session info says the cart being updated properly. I’m just not able
to output two variables:
<%= cart_item.quantity %> × <%= h(cart_item.title) %>

I did some research, and I can grasp that local variables aren’t
accessible here, but I’m not sure how to troubleshoot and find where
the problem occurs. (I’m coming from php and coldfusion, so maybe my
head is warped in the wrong direction.)

One thing that throws me is that cart_item is a class (CartItem), not
a var or a method…???

Code included below for reference.

The code starts with the Store controller:

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

The add_product method is in the Cart model:

class Cart
include Reloadable
attr_reader :items

def initialize
	@items = []
end

def add_product(product)
	existing_product = @items.find {|item| item.product == product}
	if existing_product
		existing_product.increment_quantity
	else
		@items << CartItem.new(product)
	end
end

end

The cart_item.quantity and cart_item.title come from the CartItem model:

class CartItem
include Reloadable
attr_reader :product, :quantity

def initialize(product)
	@product = product
	@quantity = 1
end

def increment_quantity
	@quantity += 1
end

def title
	@product.title
end

def price
	@product.price * @quantity
end

end

Thanks in advance for any assistance,

Austin G.
Thinking & Making: IA, UX, and IxD
http://thinkingandmaking.com
[email protected]

Austin,

You need to do:
<%= @cart_item.quantity %> × <%= h(@cart_item.title) %>

Zack

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