Agile Web Development with Rails, Edition 4

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
  # GET /carts/1
  # GET /carts/1.xml
  def show
    begin
      @cart = Cart.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error "Attempt to access invalid cart #{params[:id]}"
      redirect_to store_url, :notice => 'Invalid cart'
    else
      respond_to do |format|
        format.html # show.html.erb
        format.xml  { render :xml => @cart }
      end
    end
  end

Reproduce the error.

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

Invalid cart

Your Pragmatic Catalog

Debug

Debug It!

Professional programmers develop a knack of unerringly zeroing in on the root cause of a bug. They can do that because they've written a lot of buggy code and then gained experience fixing it. This book captures all this experience -- use it, and you'll find you write fewer bugs, and the ones you do write will become easier to hunt down.

$34.95
Ruby

Programming Ruby 1.9

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.50
Wd4d

Web Design for Developers

Web Design for Developers will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

$42.95

Inspect the log.

tail -25 log/development.log
  /home/rubys/.rvm/rubies/ruby-1.9.3-r28190/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
 
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 (4.3ms)
Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (10.5ms)
 
 
Started GET "/carts/wibble" for 127.0.0.1 at 2010-06-06 10:35:26 -0400
  Processing by CartsController#show as HTML
  Parameters: {"id"=>"wibble"}
  SQL (0.6ms)  *[1m SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
*[0m
  Cart Load (0.3ms)  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 20ms
 
 
Started GET "/" for 127.0.0.1 at 2010-06-06 10:35:27 -0400
  Processing by StoreController#index as */*
  Product Load (1.0ms)  *[1mSELECT "products".* FROM "products" ORDER BY title*[0m
Rendered store/index.html.erb within layouts/application (25.8ms)
Completed 200 OK in 67ms (Views: 42.1ms | ActiveRecord: 1.0ms)

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