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
Pragmatic Bookshelf
Your Pragmatic Catalog
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.
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.
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.
tail -99 log/development.log
Started GET "/store/add_to_cart/wibble" for 127.0.0.1 at Sun Jun 06 10:54:00 -0400 2010
Processing by StoreController#add_to_cart as HTML
Parameters: {"id"=>"wibble"}
Product Load (0.3ms) *[1mSELECT "products".* FROM "products" WHERE ("products"."id" = 0) LIMIT 1*[0m
Completed in 5ms
ActiveRecord::RecordNotFound (Couldn't find Product with ID=wibble):
/home/rubys/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:287:in `find_one'
/home/rubys/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:274:in `find_with_ids'
/home/rubys/git/rails/activerecord/lib/active_record/relation/finder_methods.rb:102:in `find'
/home/rubys/git/rails/activerecord/lib/active_record/base.rb:403:in `__send__'
/home/rubys/git/rails/activerecord/lib/active_record/base.rb:403:in `find'
/home/rubys/git/awdwr/work-188/depot/app/controllers/store_controller.rb:9:in `add_to_cart'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/implicit_render.rb:4:in `send_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/base.rb:145:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rendering.rb:11:in `process_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/callbacks.rb:18:in `process_action'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:433:in `_run__849683412__process_action__746879535__callbacks'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:408:in `send'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:408:in `_run_process_action_callbacks'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:88:in `send'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:88:in `run_callbacks'
/home/rubys/git/rails/actionpack/lib/abstract_controller/callbacks.rb:17:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:29:in `process_action'
/home/rubys/git/rails/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:48:in `__send__'
/home/rubys/git/rails/activesupport/lib/active_support/notifications.rb:48:in `instrument'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/instrumentation.rb:28:in `process_action'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rescue.rb:8:in `process_action'
/home/rubys/git/rails/actionpack/lib/abstract_controller/base.rb:114:in `process'
/home/rubys/git/rails/actionpack/lib/abstract_controller/rendering.rb:40:in `process'
/home/rubys/git/rails/actionpack/lib/action_controller/metal.rb:126:in `dispatch'
/home/rubys/git/rails/actionpack/lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
/home/rubys/git/rails/actionpack/lib/action_controller/metal.rb:166:in `action'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:27:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:27:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-mount-0.6.3/lib/rack/mount/route_set.rb:148:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-mount-0.6.3/lib/rack/mount/code_generation.rb:89:in `recognize'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-mount-0.6.3/lib/rack/mount/code_generation.rb:66:in `optimized_each'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-mount-0.6.3/lib/rack/mount/code_generation.rb:88:in `recognize'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-mount-0.6.3/lib/rack/mount/route_set.rb:139:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/routing/route_set.rb:454:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/head.rb:14:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-1.1.0/lib/rack/methodoverride.rb:24:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/params_parser.rb:21:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/flash.rb:177:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/session/abstract_store.rb:106:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/cookies.rb:235:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/query_cache.rb:31:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
/home/rubys/git/rails/activerecord/lib/active_record/query_cache.rb:11:in `cache'
/home/rubys/git/rails/activerecord/lib/active_record/query_cache.rb:30:in `call'
/home/rubys/git/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:365:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:46:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:414:in `_run_call_callbacks'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:88:in `send'
/home/rubys/git/rails/activesupport/lib/active_support/callbacks.rb:88:in `run_callbacks'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/callbacks.rb:44:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-1.1.0/lib/rack/sendfile.rb:105:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:48:in `call'
/home/rubys/git/rails/railties/lib/rails/rack/logger.rb:14:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-1.1.0/lib/rack/runtime.rb:17:in `call'
/home/rubys/git/rails/activesupport/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-1.1.0/lib/rack/lock.rb:11:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-1.1.0/lib/rack/lock.rb:11:in `synchronize'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-1.1.0/lib/rack/lock.rb:11:in `call'
/home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/static.rb:30:in `call'
/home/rubys/git/rails/railties/lib/rails/application.rb:145:in `call'
/home/rubys/git/rails/railties/lib/rails/application.rb:81:in `send'
/home/rubys/git/rails/railties/lib/rails/application.rb:81:in `method_missing'
/home/rubys/git/rails/railties/lib/rails/rack/log_tailer.rb:15:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-1.1.0/lib/rack/content_length.rb:13:in `call'
/home/rubys/.rvm/gems/ruby-1.8.8-r28169/gems/rack-1.1.0/lib/rack/handler/webrick.rb:48:in `service'
/home/rubys/.rvm/rubies/ruby-1.8.8-r28169/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/home/rubys/.rvm/rubies/ruby-1.8.8-r28169/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/home/rubys/.rvm/rubies/ruby-1.8.8-r28169/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/home/rubys/.rvm/rubies/ruby-1.8.8-r28169/lib/ruby/1.8/webrick/server.rb:162
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (12.5ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (17.2ms)
Started GET "/store/add_to_cart/wibble" for 127.0.0.1 at Sun Jun 06 10:54:00 -0400 2010
Processing by StoreController#add_to_cart as HTML
Parameters: {"id"=>"wibble"}
Product Load (0.3ms) 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 Sun Jun 06 10:54:00 -0400 2010
Processing by StoreController#index as */*
Product Load (0.9ms) SELECT "products".* FROM "products" ORDER BY title
Rendered store/index.html.erb within layouts/store (14.4ms)
Completed 200 OK in 27ms (Views: 23.2ms | ActiveRecord: 1.6ms)
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
Pragmatic Bookshelf
Invalid product
Your Pragmatic Catalog
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.
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.
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.
8.5 Iteration C4: Finishing the Cart
8.3 Iteration C2: Creating a Smarter Cart