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
Completed 404 Not Found in 0.8ms
 
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 (0.8ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.6ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (4.2ms)
 
 
Started GET "/carts/wibble" for 127.0.0.1 at 2015-05-26 00:41:07 -0400
Processing by CartsController#show as HTML
  Parameters: {"id"=>"wibble"}
  Cart Load (0.1ms)  SELECT "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", "wibble"]]
Attempt to access invalid cart wibble
Redirected to http://localhost:3000/
Completed 302 Found in 1.7ms (ActiveRecord: 0.4ms)
 
 
Started GET "/" for 127.0.0.1 at 2015-05-26 00:41:07 -0400
Processing by StoreController#index as HTML
  Product Load (0.1ms)  SELECT "products".* FROM "products" ORDER BY title
  Rendered store/index.html.erb within layouts/application (6.0ms)
Completed 200 OK in 18.1ms (Views: 14.3ms | ActiveRecord: 0.3ms)

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