Many of you probably know the ‘depot’ app from the ‘Agile Rails
development’ book.
I have constructed the ‘products’ model, the Store-controller, and the
‘Cart’ and ‘Line Item’ classes. I have told Application-controller about
:cart and :line_item:
model :cart
model :line_item
Here’s part of the Store_controller:
def add_to_cart
product = Product.find(params[:id])
@cart = find_cart
@cart.add_product(product)
#1# breakpoint “hallo”
redirect_to(:action => ‘display_cart’)
end
def display_cart
@cart = find_cart
@items = @cart.items
end
private
def find_cart
#2# session[:cart] ||= Cart.new
end
#1# At this breakpoint I can verify that the cart contains the correct
product. There’s a difficulty with inspection, however: when I run the
breakpointer, the DOS-window seems to change characteristic, so that I
can’t find the ‘@’ on the keyboard, not even ‘escape’ it with ‘alt-64’.
So, instead of inspecting @items directly, I instead have to inspect
‘find_cart.items,’ but that seems to work fine. However, this stops me
from breakpointing the ‘display_cart’ method, just because of the ‘@’
problem!!
The cart is already assigned to the session, see #2#.
Then the view changes to the test-stub ‘display_cart,’ which looks like
this:
Display Cart
Your cart contains <%= @items.size %> items.
Every time, no matter how many times I back up and add products, the
@items.size evaluates to ZERO. So the session and the cart get lost.
Both my browsers have cookies enabled, of course. (Though, in the
cookie-list, I can’t find any cookie which associates to this
application).
I’m totally stuck here. I’ve checked the code against the source one
million times. I’m beginning to distrust Rails (but from experience, I
know that most often, there’s usually some stupid bug that’s all my own
fault :-))
Any suggestions?
Regards, Audun.