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

<(?-mix:Unpermitted parameters?: cart_id)> expected but was
<  rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart>.
<0> expected to be
>=
<1>.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:174:in `block in <class:DepotTest>'

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
  Rendered /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/templates/_markup.html.erb (0.3ms)
  Rendered /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
  Rendered /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
  Rendered /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.2ms)
  Rendered /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/templates/console.js.erb within layouts/javascript (10.5ms)
  Rendered /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
  Rendered /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.3ms)
  Rendered /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/templates/index.html.erb (21.8ms)
 
 
Started GET "/carts/wibble" for 127.0.0.1 at 2015-03-30 06:45:27 -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/
Completed 302 Found in 4ms (ActiveRecord: 0.4ms)
 
 
Started GET "/" for 127.0.0.1 at 2015-03-30 06:45:27 -0400
Processing by StoreController#index as HTML
  Product Load (0.2ms)  SELECT  "products".* FROM "products" ORDER BY "products"."updated_at" DESC LIMIT 1
  Product Load (0.1ms)  SELECT "products".* FROM "products" ORDER BY "products"."title" ASC
  Rendered store/index.html.erb within layouts/application (6.5ms)
Completed 200 OK in 61ms (Views: 58.7ms | ActiveRecord: 0.4ms)

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
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from block in <top (required)> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/extensions.rb:18)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from block in <top (required)> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/extensions.rb:18)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rake aborted!
SyntaxError: /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/line_items_controller_test.rb:25: syntax error, unexpected keyword_end, expecting '}'
/home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/line_items_controller_test.rb:55: syntax error, unexpected end-of-input, expecting keyword_end
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:274:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:274:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:274:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:118:in `block in run_tests'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:117:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:117:in `run_tests'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:88:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:82:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:26:in `block (3 levels) in <top (required)>'
Tasks: TOP => test:controllers
(See full trace by running task with --trace)

Inspect the log.

grep -B 8 -A 7 "Unpermitted parameter" log/test.log
edit test/controllers/line_items_controller_test.rb
  test "should update line_item" do
    patch :update, params: { 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
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
rake test:controllers
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from rescue in <class:Exception> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/integration/cruby.rb:37)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from block in <top (required)> at /home/rubys/.rvm/gems/ruby-head-n50123/gems/web-console-2.1.2/lib/web_console/extensions.rb:18)
DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super. (called from included at /home/rubys/.rvm/gems/ruby-head-n50123/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rake aborted!
SyntaxError: /home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/line_items_controller_test.rb:25: syntax error, unexpected keyword_end, expecting '}'
/home/rubys/git/awdwr/edition4/work-220/depot/test/controllers/line_items_controller_test.rb:59: syntax error, unexpected end-of-input, expecting keyword_end
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:274:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:274:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:274:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:118:in `block in run_tests'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:117:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:117:in `run_tests'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:88:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/runner.rb:82:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:26:in `block (3 levels) in <top (required)>'
Tasks: TOP => test:controllers
(See full trace by running task with --trace)
grep "Unpermitted parameters" log/test.log | wc -l
0

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