Activity 1

Change the application so that clicking a book’s image will also invoke the create action.

Hint: the first parameter to link_to is placed in the generated <a> tag, and the Rails helper image_tag constructs an HTML <img> tag. Include a call to it as the first parameter to a link_to call. Be sure to include :method => :post in your html_options on your call to link_to.

Discuss

Activity 2

Add a new variable to the session to record how many times the user has accessed the index action.

Hint: 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

Activity 3

Pass this counter to your template, and display it at the top of the catalog page.

Hint: the pluralize helper might be useful when forming the message you display.

Discuss

Activity 4

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

Hint: adding something to a cart doesn’t modify the Cart, instead look to add this logic to the create method of the line_items_controller.

Discuss

Activity 5

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

Hint: While this can be done inside the template, it should be done in the controller.

Discuss

Comments

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.

Discuss