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.json
  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.json { render json: @cart }
      end
    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

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
  Cart Load (0.2ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", "wibble"]]
Completed 404 Not Found in 3ms
 
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.7ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (6.8ms)
 
 
Started GET "/carts/wibble" for 127.0.0.1 at 2011-10-29 03:15:32 -0400
Processing by CartsController#show as HTML
  Parameters: {"id"=>"wibble"}
  Cart Load (0.2ms)  �[1mSELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1�[0m  [["id", "wibble"]]
Attempt to access invalid cart wibble
Redirected to http://localhost:3000/
Completed 302 Found in 4ms (ActiveRecord: 0.7ms)
 
 
Started GET "/" for 127.0.0.1 at 2011-10-29 03:15:32 -0400
Processing by StoreController#index as HTML
  Product Load (0.2ms)  SELECT "products".* FROM "products" ORDER BY title
  Rendered store/index.html.erb within layouts/application (18.2ms)
Completed 200 OK in 41ms (Views: 28.6ms | ActiveRecord: 1.1ms)

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