Agile Web Development with Rails, Edition 4

10.3 Iteration E3: Finishing the Cart 10.1 Iteration E1: Creating a Smarter Cart

10.2 Iteration E2: Handling Errors

Log errors and show them on the screen.

Rescue error: log, flash, and redirect.

edit app/controllers/carts_controller.rb
class CartsController < ApplicationController
  rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
  # GET /carts
    def invalid_cart
      logger.error "Attempt to access invalid cart #{params[:id]}"
      redirect_to store_url, notice: 'Invalid cart'
    end
end

Reproduce the error.

get /carts/wibble
You are being redirected.
get http://localhost:3000/

Invalid cart

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$36.00
Ruby

Programming Ruby 1.9 & 2.0

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95

Inspect the log.

tail -25 log/development.log
ActiveRecord::RecordNotFound (Couldn't find Cart with ID=wibble):
  app/controllers/carts_controller.rb:16:in `show'
 
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.3ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (6.0ms)
 
 
Started GET "/carts/wibble" for 127.0.0.1 at 2014-02-04 15:42:04 -0500
  Processing by CartsController#show as HTML
  Parameters: {"id"=>"wibble"}
  SQL (0.3ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  Cart Load (0.1ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = 0 LIMIT 1
Attempt to access invalid cart wibble
Redirected to http://localhost:3000/
Completed 302 Found in 5ms
 
 
Started GET "/" for 127.0.0.1 at 2014-02-04 15:42:04 -0500
  Processing by StoreController#index as HTML
  Product Load (0.4ms)  SELECT "products".* FROM "products" ORDER BY title
Rendered store/index.html.erb within layouts/application (10.6ms)
Completed 200 OK in 20ms (Views: 12.7ms | ActiveRecord: 0.8ms)

10.3 Iteration E3: Finishing the Cart 10.1 Iteration E1: Creating a Smarter Cart