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

Question: 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? Thanks!

Question 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??

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

Question: On page 132 is anyone having problems adding the total_price method to the cart model? I’m getting an error, “undefined method ‘total_price’” from the cart view show.html.erb. It’s there, anyone? Answer to question found on forum


Question Sorry – not sure this is in the right place but I could not find the playtime section for this: I tried the image create linking on playtime and was able to do this: <%= link_to(image_tag(product.image_url), line_items_path(:product_id => product, :method => :post )) %> It goes to the line item view… Has anyone got it to goto the cart view like on the button_to link?

Answer I noticed that you’d declared the :method inside the line_items_path object. Give it its own space, like so:

<%= link_to image_tag(product.image_url), 
                  line_items_path(:product_id => product), :method => :post %>