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
  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_index_url, notice: 'Invalid cart'
    end
end

Reproduce the error.

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

Invalid cart

Your Pragmatic Catalog

Dcbang

Rails, Angular, Postgres, and Bootstrap

Powerful, Effective, and Efficient Full-Stack Web Development As a Rails developer, you care about user experience and performance, but you also want simple and maintainable code. Achieve all that by embracing the full stack of web development, from styling with Bootstrap, building an interactive user interface with AngularJS, to storing data quickly and reliably in PostgreSQL. Take a holistic view of full-stack development to create usable, high-performing applications, and learn to use these technologies effectively in a Ruby on Rails environment.

$45.00
Adrpo

Ruby Performance Optimization

Why Ruby Is Slow, and How to Fix It You don’t have to accept slow Ruby or Rails performance. In this comprehensive guide to Ruby optimization, you’ll learn how to write faster Ruby code—but that’s just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you through perils of memory and CPU optimization, profiling, measuring, performance testing, garbage collection, and tuning. You’ll find that all those “hard” things aren’t so difficult after all, and your code will run orders of magnitude faster.

$46.00
7apps

Seven Mobile Apps in Seven Weeks

Native Apps, Multiple Platforms Answer the question “Can we build this for ALL the devices?” with a resounding YES. This book will help you get there with a real-world introduction to seven platforms, whether you’re new to mobile or an experienced developer needing to expand your options. Plus, you’ll find out which cross-platform solution makes the most sense for your needs.

$26.00

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

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
rake test:controllers
Run options: --seed 8239
 
# Running:
 
.......................
 
Finished in 0.224947s, 102.2463 runs/s, 208.9380 assertions/s.
 
23 runs, 47 assertions, 0 failures, 0 errors, 0 skips

Inspect the log.

grep -B 8 -A 7 "Unpermitted parameter" log/test.log
   (0.0ms)  begin transaction
-----------------------------------------------------
LineItemsControllerTest: test_should_update_line_item
-----------------------------------------------------
  LineItem Load (0.0ms)  SELECT  "line_items".* FROM "line_items"  WHERE "line_items"."id" = ? LIMIT 1  [["id", 980190962]]
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 1  [["id", 980190962]]
Unpermitted parameters: cart_id
   (0.0ms)  SAVEPOINT active_record_1
   (0.0ms)  RELEASE SAVEPOINT active_record_1
Redirected to http://test.host/line_items/980190962
Completed 302 Found in 1ms (ActiveRecord: 0.1ms)
   (0.0ms)  rollback transaction
   (0.0ms)  begin transaction
---------------------------------------------------------
edit test/controllers/line_items_controller_test.rb
  test "should update line_item" do
    patch :update, id: @line_item, line_item: { product_id: @line_item.product_id }
    assert_redirected_to line_item_path(assigns(:line_item))
  end
rake log:clear LOGS=test
rake test:controllers
Run options: --seed 9940
 
# Running:
 
.......................
 
Finished in 0.229604s, 100.1724 runs/s, 204.7002 assertions/s.
 
23 runs, 47 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