The Depot Application

The Depot Application

8.5 Iteration C4: Finishing the Cart 8.3 Iteration C2: Creating a Smarter Cart

8.4 Iteration C3: Handling Errors

edit app/controllers/store_controller.rb
  def add_to_cart
    product = Product.find(params[:id])
    @cart = find_cart
    @cart.add_product(product)
  rescue ActiveRecord::RecordNotFound
    logger.error("Attempt to access invalid product #{params[:id]}")
    flash[:notice] = "Invalid product"
    redirect_to :action => 'index'
  end
get /store/add_to_cart/wibble
You are being redirected.
get http://localhost:3000/store/index

Your Pragmatic Catalog

Auto

Pragmatic Project Automation

Pragmatic Project Automation shows you how to improve the consistency and repeatability of your project's procedures using automation to reduce risk and errors.

Simply put, we're going to put this thing called a computer to work for you doing the mundane (but important) project stuff. That means you'll have more time and energy to do the really exciting---and difficult---stuff, like writing quality code.

$29.95
Utc

Pragmatic Unit Testing (C#)

Pragmatic programmers use feedback to drive their development and personal processes. The most valuable feedback you can get while coding comes from unit testing.

Without good tests in place, coding can become a frustrating game of "whack-a-mole." That's the carnival game where the player strikes at a mechanical mole; it retreats and another mole pops up on the opposite side of the field. The moles pop up and down so fast that you end up flailing your mallet helplessly as the moles continue to pop up where you least expect them.

$27.75
Svn

Pragmatic Version Control

This book is a recipe-based approach to using Subversion that will get you up and running quickly---and correctly. All projects need version control: it's a foundational piece of any project's infrastructure. Yet half of all project teams in the U.S. don't use any version control at all. Many others don't use it well, and end up experiencing time-consuming problems.

$28.50
tail -99 log/development.log
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
*[0m
  Product Load (0.5ms)  SELECT "products".* FROM "products" WHERE ("products"."id" = 2) LIMIT 1
Completed   in 137ms
 
ActionView::MissingTemplate (Missing template store/add_to_cart with {:formats=>[:html], :handlers=>[:builder, :rjs, :rhtml, :erb, :rxml], :locale=>[:en, :en]} in view paths "/home/rubys/git/awdwr/work-188/depot/app/views"):
 
 
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (1.3ms)
 
 
Started GET "/store/add_to_cart/2" for 127.0.0.1 at Sat Aug 07 09:14:40 -0400 2010
  Processing by StoreController#add_to_cart as HTML
  Parameters: {"id"=>"2"}
  Product Load (0.5ms)  *[1mSELECT "products".* FROM "products" WHERE ("products"."id" = 2) LIMIT 1*[0m
Rendered store/add_to_cart.html.erb within layouts/store (11.6ms)
Completed 200 OK in 50ms (Views: 17.8ms | ActiveRecord: 0.8ms)
 
 
Started GET "/store/add_to_cart/3" for 127.0.0.1 at Sat Aug 07 09:14:40 -0400 2010
  Processing by StoreController#add_to_cart as HTML
  Parameters: {"id"=>"3"}
  Product Load (0.5ms)  SELECT "products".* FROM "products" WHERE ("products"."id" = 3) LIMIT 1
Rendered store/add_to_cart.html.erb within layouts/store (11.5ms)
Completed 200 OK in 26ms (Views: 17.7ms | ActiveRecord: 1.7ms)
 
 
Started GET "/store/add_to_cart/2" for 127.0.0.1 at Sat Aug 07 09:14:41 -0400 2010
  Processing by StoreController#add_to_cart as HTML
  Parameters: {"id"=>"2"}
  Product Load (0.5ms)  *[1mSELECT "products".* FROM "products" WHERE ("products"."id" = 2) LIMIT 1*[0m
Completed   in 131ms
 
NoMethodError (undefined method `product' for #<Product:0xb6898e68>):
  app/models/cart.rb:10:in `add_product'
  app/models/cart.rb:10:in `each'
  app/models/cart.rb:10:in `find'
  app/models/cart.rb:10:in `add_product'
  app/controllers/store_controller.rb:11:in `add_to_cart'
 
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.9ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (21.1ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (31.5ms)
  SQL (5.1ms)  *[1mDELETE FROM sessions*[0m
 
 
Started GET "/store/add_to_cart/2" for 127.0.0.1 at Sat Aug 07 09:14:44 -0400 2010
  Processing by StoreController#add_to_cart as HTML
  Parameters: {"id"=>"2"}
  Product Load (0.3ms)  SELECT "products".* FROM "products" WHERE ("products"."id" = 2) LIMIT 1
Rendered store/add_to_cart.html.erb within layouts/store (7.1ms)
Completed 200 OK in 30ms (Views: 11.1ms | ActiveRecord: 1.0ms)
 
 
Started GET "/store/add_to_cart/2" for 127.0.0.1 at Sat Aug 07 09:14:44 -0400 2010
  Processing by StoreController#add_to_cart as HTML
  Parameters: {"id"=>"2"}
  Product Load (0.3ms)  *[1mSELECT "products".* FROM "products" WHERE ("products"."id" = 2) LIMIT 1*[0m
Rendered store/add_to_cart.html.erb within layouts/store (1.7ms)
Completed 200 OK in 16ms (Views: 5.4ms | ActiveRecord: 1.0ms)
 
 
Started GET "/store/add_to_cart/3" for 127.0.0.1 at Sat Aug 07 09:14:44 -0400 2010
  Processing by StoreController#add_to_cart as HTML
  Parameters: {"id"=>"3"}
  Product Load (0.3ms)  SELECT "products".* FROM "products" WHERE ("products"."id" = 3) LIMIT 1
Rendered store/add_to_cart.html.erb within layouts/store (1.7ms)
Completed 200 OK in 16ms (Views: 5.6ms | ActiveRecord: 1.0ms)
 
 
Started GET "/store/add_to_cart/wibble" for 127.0.0.1 at Sat Aug 07 09:14:44 -0400 2010
  Processing by StoreController#add_to_cart as HTML
  Parameters: {"id"=>"wibble"}
  Product Load (0.2ms)  *[1mSELECT "products".* FROM "products" WHERE ("products"."id" = 0) LIMIT 1*[0m
Completed   in 5ms
 
ActiveRecord::RecordNotFound (Couldn't find Product with ID=wibble):
  app/controllers/store_controller.rb:9:in `add_to_cart'
 
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.7ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (100.9ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (106.8ms)
 
 
Started GET "/store/add_to_cart/wibble" for 127.0.0.1 at Sat Aug 07 09:14:45 -0400 2010
  Processing by StoreController#add_to_cart as HTML
  Parameters: {"id"=>"wibble"}
  Product Load (0.2ms)  SELECT "products".* FROM "products" WHERE ("products"."id" = 0) LIMIT 1
Attempt to access invalid product wibble
Redirected to http://localhost:3000/store/index
Completed 302 Found in 6ms
 
 
Started GET "/store/index" for 127.0.0.1 at Sat Aug 07 09:14:45 -0400 2010
  Processing by StoreController#index as HTML
  Product Load (0.5ms)  *[1mSELECT "products".* FROM "products" ORDER BY title*[0m
Rendered store/index.html.erb within layouts/store (14.6ms)
Completed 200 OK in 23ms (Views: 18.3ms | ActiveRecord: 2.8ms)
edit app/views/layouts/store.html.erb
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>Pragprog Books Online Store</title>
  <%= stylesheet_link_tag "depot", :media => "all" %>
</head>
<body id="store">
  <div id="banner">
    <%= image_tag("logo.png") %>
    <%= @page_title || "Pragmatic Bookshelf" %>
  </div>
  <div id="columns">
    <div id="side">
      <a href="http://www....">Home</a><br />
      <a href="http://www..../faq">Questions</a><br />
      <a href="http://www..../news">News</a><br />
      <a href="http://www..../contact">Contact</a><br />
    </div>
    <div id="main">
      <% if flash[:notice] -%>
        <div id="notice"><%= flash[:notice] %></div>
      <% end -%>
 
      <%= yield :layout %>
    </div>
  </div>
</body>
</html>
edit public/stylesheets/depot.css
#notice {
  border: 2px solid red;
  padding: 1em;
  margin-bottom: 2em;
  background-color: #f0f0f0;
  font: bold smaller sans-serif;
}
get /store/add_to_cart/wibble
You are being redirected.
get http://localhost:3000/store/index
Invalid product

Your Pragmatic Catalog

Auto

Pragmatic Project Automation

Pragmatic Project Automation shows you how to improve the consistency and repeatability of your project's procedures using automation to reduce risk and errors.

Simply put, we're going to put this thing called a computer to work for you doing the mundane (but important) project stuff. That means you'll have more time and energy to do the really exciting---and difficult---stuff, like writing quality code.

$29.95
Utc

Pragmatic Unit Testing (C#)

Pragmatic programmers use feedback to drive their development and personal processes. The most valuable feedback you can get while coding comes from unit testing.

Without good tests in place, coding can become a frustrating game of "whack-a-mole." That's the carnival game where the player strikes at a mechanical mole; it retreats and another mole pops up on the opposite side of the field. The moles pop up and down so fast that you end up flailing your mallet helplessly as the moles continue to pop up where you least expect them.

$27.75
Svn

Pragmatic Version Control

This book is a recipe-based approach to using Subversion that will get you up and running quickly---and correctly. All projects need version control: it's a foundational piece of any project's infrastructure. Yet half of all project teams in the U.S. don't use any version control at all. Many others don't use it well, and end up experiencing time-consuming problems.

$28.50

8.5 Iteration C4: Finishing the Cart 8.3 Iteration C2: Creating a Smarter Cart