Home arrow Ruby-on-Rails arrow Page 2 - Finishing a Shopping Cart Implementation

Finishing a Shopping Cart Implementation

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

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
June 04, 2010
  1. · Finishing a Shopping Cart Implementation
  2. · Implementing the Add Items to the Cart User Story
  3. · Ajax’ing It
  4. · Drag-and-Drop
  5. · Implementing the Remove Items from the Cart User Story
  6. · Implementing the Clear the Cart User Story

print this article

Finishing a Shopping Cart Implementation - Implementing the Add Items to the Cart User Story
(Page 2 of 6 )

We’re going to use two approaches to implementing the Add Items to the Cart user story. One is the “traditional” way, which takes care of users whose browser doesn’t support JavaScript. The other way will use Ajax techniques to provide a streamlined user interface.

The Traditional Way

We need to implement the action for adding an item to a cart in ourCartController (in
app/controllers/cart_controller.rb), as follows:

def add
  @book = Book.find(params[:id])

  if request.post?
    @item = @cart.add(params[:id])
    flash[:cart_notice] = "Added <em>#{@item.book.title}</em>"
    redirect_to :controller => "catalog"

The code checks whether the request was an HTTPPOSTorGETrequest and acts on that information. In the case of a formPOST, we add the item, insert a notice to theflashhash, and redirect to the catalog controller. If the user got to the action by clicking a link, we show her a confirmation page with a form that points to the same action.

Createapp/views/cart/add.rhtmland add the following to it to implement the confirmation page:

<strong>Please confirm adding <em><%= @book.title %></em>
to your shopping cart.</strong>
<%= button_to "Confirm", :action => "add", :id => params[:id] %>

button_tois a Rails helper that behaves just likelink_to, but instead of an anchor element, it creates an inline form with a single button. When a user clicks this button, she gets to the sameadd action, this time with aPOSTrequest, and will get the new item added to the cart. This might sound awkward, but there are two good reasons to make the application behave like this:

  1. You should never useGET requests (normal links) to change the state of a web application. So-called web accelerators (or link fetchers) often crawl through all the links of a loaded web page and fetch the linked pages while the user is reading the page. If fetching those links would result in adding random items to the shopping cart, we would probably end up with a bunch of less-than-happy customers.
  2. This traditional way to add items to a shopping cart will be visible to only the few customers who are using a browser that doesn’t support JavaScript. In the next section, we will implement an interface that will be used by the vast majority of customers, and that is about gazillion times slicker than the approach described in this section.

Now adding an item to a cart almost works. However, there is noaddmethod in theCartclass, so we need to add it toapp/models/cart.rbbefore we can start filling up the carts.

def add(book_id)
items = cart_items.find_all_by_book_id(book_id)
  book = Book.find(book_id)

  if items.size < 1
ci = cart_items.create(:book_id => book_id,
:amount => 1,
:price => book.price)
    ci = items.first
    ci.update_attribute(:amount, ci.amount + 1)

The idea behind theaddmethod is that if there is already a certain amount of the current book in the cart, we increment that amount by one, using theupdate_attributemethod. If there is no sign of the given book, a new item will be added and its amount will be set to1.

We’re now finished with the old-fashioned way of adding books to the cart. Our test runs fine (try if you don’t believe us), so we can extend our application to work with Ajax.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials