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.3ms)  SELECT "products".* FROM "products" WHERE "products"."id" = 2 LIMIT 1
Completed 500 Internal Server Error in 63ms
 
ActionView::MissingTemplate (Missing template store/add_to_cart with {:handlers=>[:rxml, :builder, :rjs, :erb, :rhtml], :formats=>[:html], :locale=>[:en, :en]} in view paths "/home/rubys/git/awdwr/edition3/work-30/depot/app/views"):
 
 
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.9ms)
 
 
Started GET "/store/add_to_cart/2" for 127.0.0.1 at Fri Jun 29 15:12:06 -0400 2012
  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 (7.5ms)
Completed 200 OK in 25ms (Views: 12.6ms | ActiveRecord: 0.5ms)
 
 
Started GET "/store/add_to_cart/3" for 127.0.0.1 at Fri Jun 29 15:12:06 -0400 2012
  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 (6.7ms)
Completed 200 OK in 14ms (Views: 11.3ms | ActiveRecord: 1.1ms)
 
 
Started GET "/store/add_to_cart/2" for 127.0.0.1 at Fri Jun 29 15:12:07 -0400 2012
  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
Completed 500 Internal Server Error in 9ms
 
NoMethodError (undefined method `product' for #<Product:0x7f418484c188>):
  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 (1.9ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (8.4ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.2ms)
  SQL (174.4ms)  *[1mDELETE FROM sessions*[0m
 
 
Started GET "/store/add_to_cart/2" for 127.0.0.1 at Fri Jun 29 15:12:11 -0400 2012
  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.6ms)
Completed 200 OK in 25ms (Views: 12.9ms | ActiveRecord: 1.1ms)
 
 
Started GET "/store/add_to_cart/2" for 127.0.0.1 at Fri Jun 29 15:12:12 -0400 2012
  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 14ms (Views: 6.3ms | ActiveRecord: 0.9ms)
 
 
Started GET "/store/add_to_cart/3" for 127.0.0.1 at Fri Jun 29 15:12:12 -0400 2012
  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 14ms (Views: 6.2ms | ActiveRecord: 0.8ms)
 
 
Started GET "/store/add_to_cart/wibble" for 127.0.0.1 at Fri Jun 29 15:12:12 -0400 2012
  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 404 Not Found in 3ms
 
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.8ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (14.9ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (102.3ms)
 
 
Started GET "/store/add_to_cart/wibble" for 127.0.0.1 at Fri Jun 29 15:12:13 -0400 2012
  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 4ms
 
 
Started GET "/store/index" for 127.0.0.1 at Fri Jun 29 15:12:13 -0400 2012
  Processing by StoreController#index as HTML
  Product Load (1.0ms)  *[1mSELECT "products".* FROM "products" ORDER BY title*[0m
Rendered store/index.html.erb within layouts/store (27.2ms)
Completed 200 OK in 41ms (Views: 36.0ms | ActiveRecord: 3.3ms)
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