Agile Web Development with Rails, Edition 5

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
  before_action :set_cart, only: [:show, :edit, :update, :destroy]
  rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart
  # GET /carts
  # ...
  private
  # ...
 
    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:67:in `set_cart'
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.1ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.7ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (14.0ms)
Started GET "/carts/wibble" for 127.0.0.1 at 2016-05-06 21:42:10 -0400
Processing by CartsController#show as HTML
  Parameters: {"id"=>"wibble"}
  Cart Load (0.1ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ?  [["id", 0], ["LIMIT", 1]]
Attempt to access invalid cart wibble
Redirected to http://localhost:3000/
Completed 302 Found in 5ms (ActiveRecord: 0.6ms)
 
 
Started GET "/" for 127.0.0.1 at 2016-05-06 21:42:10 -0400
Processing by StoreController#index as HTML
  Product Load (0.2ms)  SELECT  "products".* FROM "products" ORDER BY "products"."updated_at" DESC LIMIT ?  [["LIMIT", 1]]
  Product Load (0.1ms)  SELECT "products".* FROM "products" ORDER BY "products"."title" ASC
  Rendered store/index.html.erb within layouts/application (10.0ms)
Completed 200 OK in 34ms (Views: 30.0ms | ActiveRecord: 0.6ms)
 
 

Limit access to product_id

edit app/controllers/line_items_controller.rb
    # Never trust parameters from the scary internet, only allow the white
    # list through.
    def line_item_params
      params.require(:line_item).permit(:product_id)
    end
rails test:controllers
Run options: --seed 30827
 
# Running:
 
.......................
 
Finished in 0.445656s, 51.6093 runs/s, 98.7308 assertions/s.
 
23 runs, 44 assertions, 0 failures, 0 errors, 0 skips

Inspect the log.

grep -B 8 -A 7 "Unpermitted parameter" log/test.log
-----------------------------------------------------
LineItemsControllerTest: test_should_update_line_item
-----------------------------------------------------
  LineItem Load (0.1ms)  SELECT  "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT ?  [["id", 980190962], ["LIMIT", 1]]
Started PATCH "/line_items/980190962" for 127.0.0.1 at 2016-05-06 21:42:12 -0400
Processing by LineItemsController#update as HTML
  Parameters: {"line_item"=>{"cart_id"=>"980190962", "product_id"=>"298486374"}, "id"=>"980190962"}
  LineItem Load (0.0ms)  SELECT  "line_items".* FROM "line_items" WHERE "line_items"."id" = ? LIMIT ?  [["id", 980190962], ["LIMIT", 1]]
Unpermitted parameter: cart_id
   (0.0ms)  SAVEPOINT active_record_1
  Product Load (0.0ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT ?  [["id", 298486374], ["LIMIT", 1]]
  Cart Load (0.0ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT ?  [["id", 980190962], ["LIMIT", 1]]
   (0.0ms)  RELEASE SAVEPOINT active_record_1
Redirected to http://www.example.com/line_items/980190962
Completed 302 Found in 3ms (ActiveRecord: 0.2ms)
   (0.0ms)  rollback transaction
edit test/controllers/line_items_controller_test.rb
  test "should update line_item" do
    patch line_item_url(@line_item),
      params: { line_item: { product_id: @line_item.product_id } }
    assert_redirected_to line_item_path(@line_item)
  end
rake log:clear LOGS=test
rails test:controllers
Run options: --seed 4755
 
# Running:
 
.......................
 
Finished in 0.441852s, 52.0537 runs/s, 99.5809 assertions/s.
 
23 runs, 44 assertions, 0 failures, 0 errors, 0 skips
grep "Unpermitted parameters" log/test.log | wc -l
0

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