Home arrow Ruby-on-Rails arrow Page 2 - Shopping Cart Implementation
RUBY-ON-RAILS

Shopping Cart Implementation


Continuing our series on setting up an online bookstore with Ruby-on-Rails, this two-part tutorial explains how to implement the shopping cart. It 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 / 9
May 28, 2010
TABLE OF CONTENTS:
  1. · Shopping Cart Implementation
  2. · Setting Up the Shopping Cart
  3. · Creating the Models
  4. · Modifying the Controller

print this article
SEARCH DEVARTICLES

Shopping Cart Implementation - Setting Up the Shopping Cart
(Page 2 of 4 )

Once again, we start our sprint by creating a test case for adding an item to the shopping cart. This time, we will use the Rails functional tests. We could use integration tests, but since we are testing the functionality of a single controller (which we will create first) in this case, we agree to use functional tests for this sprint.

Creating the Controller

Our first step is to use the script/generatecommand to create the newCartcontroller, as follows:  

$ script/generate controller Cart 

exists

app/controllers/

exists

app/helpers/

create

app/views/cart

exists

test/functional/

create

app/controllers/cart_controller.rb

create

test/functional/cart_controller_test.rb

create

app/helpers/cart_helper.rb

Creating a new controller for our shopping cart also provides us with a stub file for the functional tests.

Adding a Functional Test

Open test/functional/cart_controller_test.rband replace the dummytest_truthmethod with the following test case:  

def test_adding
  assert_difference(CartItem, :count) do
    post :add, :id => 4
  end

  assert_response :redirect
  assert_redirected_to :controller => "catalog"
  assert_equal 1, Cart.find(@request.session[:cart_id]).cart_items.size
end

In this test, we first check that posting a form to theaddaction actually creates a newCartItem. Then we check that after the form post, we are redirected to the catalog controller and that the current shopping cart is populated with an item.

If you canít figure out whatís happening here, donít worry. Once we start implementing the shopping cart, it will be clear how the test works.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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