Task C: Cart Creation

Add a new variable to the session to record how many times the user has accessed the index action. (The first time through, your count won’t be in the session. You can test for this with code like

if session[:counter].nil? 

If the session variable isn’t there, you’ll need to initialize it. Then you’ll be able to increment it. Discuss

Pass this counter to your template, and display it at the top of the catalog page. Hint: the pluralize helper (described on page 474) might be useful when forming the message you display. Discuss

Reset the counter to zero whenever the user adds something to the cart. Discuss

Change the template to display the counter only if it is greater than five. Discuss

It will be more fun to put the counter message in the error display area using flash. The requirements are: if there is no error, just display the counter; but if there is an error, then display the error and the counter. When the index page is refreshed, remove the error and update the counter. (Hint: use a session variable to keep track of whether there is a redirect.)

Has anyone run into the issue where the cart doesn't show up when you first go to the page (before adding any products?) For example, add some products to your card, close your browser, reopen it and navigate to the /store index page, the cart won't be displayed. Add another product and the cart will show up and contain both new product and the one you've added previously. Same with the checkout page, no luck getting it to display there either. Any ideas?

I am stuck on cart creation. I click the Add to Cart button and nothing happens. I tried loading add_to_cart.html.erb directly and rails tells me:
No route matches "/add_to_cart" with {:method=>:get}
What am I doing wrong??

I am stuck in a different spot. I've just completed working through the code and have reached the point of resetting the session (page 114) and now I receive this error:
uninitialized constant Cart::CartItem

The trace displayed shows:
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:493:in ‘const_missing’
app/models/cart.rb:14:in ‘add_product’
app/controllers/store_controller.rb:11:in ‘add_to_cart’

Any ideas?

* Nevermind * Found the answer in the errata for the book during a search. I had mis-named my cart_item.rb file.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License