In this second part of a two-part series on implementing a shopping cart for an online bookstore, we'll create a floating cart that will show its contents to the user, and set up its capabilities. This article is excerpted from chapter five of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).
Finishing a Shopping Cart Implementation - Implementing the Clear the Cart User Story (Page 6 of 6 )
Clearing the cart follows pretty much the same path as the two previous user stories. We first add two functional tests to test/functional/cart_controller_test.rb, to test clearing the cart with both a normalPOSTrequest and anXmlHttpRequest.
def test_clearing post :add, :id => 4 assert_equal [Book.find(4)], Cart.find(@request.session[:cart_id]).books
post :clear assert_response :redirect assert_redirected_to :controller => "catalog" assert_equal , Cart.find(@request.session[:cart_id]).books end
def test_clearing_with_xhr post :add, :id => 4 assert_equal [Book.find(4)], Cart.find(@request.session[:cart_id]).books
xhr :post, :clear assert_response :success assert_equal 0, Cart.find(@request.session[:cart_id]).cart_items.size end
Then we add a new action calledclear toapp/controllers/cart_controller.rb, following along the lines of theaddandremove actions.
def clear if request.xhr? @cart.cart_items.destroy_all flash.now[:cart_notice] = "Cleared the cart" render :action => "clear_with_ajax" elsif request.post? @cart.cart_items.destroy_all flash[:cart_notice] = "Cleared the cart" redirect_to :controller => "catalog" else render end end
Next, we add the view for the non-Ajax way of clearing the cart,cart/clear.rhtml.
All that the template does is to replace the whole shopping cart with a new, empty one, rendered by thecart/_cart.rhtmlpartial. Note that since we’re re-rendering the wholeshopping_cartelement with a partial, we don’t need to explicitly show thecart_notificationdivision (as we did with theremove_with_ajaxtemplate), because it’s already part of the_cart.rhtmlpartial template.
We’ll also add a link for clearing the cart to the cart partial, but only if the cart is not empty. Remember that we created theclear_cart_linkhelper earlier in this chapter (see Listing 5-3).
<% if flash[:cart_notice] %> <%= render :partial => "cart/cart_notice" %> <% end %>
Our application can now be used to add items to the shopping cart, remove them from it, and clear the whole cart with a single click.
The next natural step would be to implement the functionality of checking out—that is, finalizing the order. However, since the checkout is such a beast with credit card processing and all, we’ll postpone that for now and give you all the gory details in Chapter 9.
In this chapter, we implemented a shopping cart for an online store. We used the Ajax capabilities in Ruby on Rails to implement a fast and interactive, but also backward-compatible shopping cart.
In the course of implementing the shopping cart, we showed you how to use thehas_many :throughassociation join models with ActiveRecord classes and how to use the Rails controller filters and store information in thesessionhash. You saw how to put Rails helpers likepluralizeto use and how to write your own link helpers.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.