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

Expected at least 1 element matching "a[href='http://localhost:3000/']", found 0.
<0> expected to be
>=
<1>.

Traceback:
  /home/rubys/git/awdwr/edition4/checkdepot.rb:178: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

ActiveRecord::RecordNotFound in CartsController#show

Couldn't find Cart with 'id'=wibble

Extracted source (around line #75):
73
74
75
76
77
78
              
#END:setup# Use callbacks to share common setup or constraints between actions.
def set_cart
@cart = Cart.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.

Rails.root: /home/rubys/git/awdwr/edition4/work-230/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

{"id"=>"wibble"}

Response

Headers:

None

Inspect the log.

tail -25 log/development.log
  Rendered /home/rubys/git/web-console/lib/web_console/templates/index.html.erb (15.8ms)
 
 
Started GET "/carts/wibble" for 127.0.0.1 at 2016-01-27 15:54:21 -0500
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
Completed 500 Internal Server Error in 17ms (ActiveRecord: 0.6ms)
 
ActiveRecord::RecordNotFound (Couldn't find Cart with 'id'=wibble):
  app/controllers/carts_controller.rb:75:in `set_cart'
 
 
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (2.1ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.1ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.5ms)
  Rendered /home/rubys/git/rails/actionpack/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (12.0ms)
  Rendered /home/rubys/git/web-console/lib/web_console/templates/_markup.html.erb (0.2ms)
  Rendered /home/rubys/git/web-console/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.2ms)
  Rendered /home/rubys/git/web-console/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.1ms)
  Rendered /home/rubys/git/web-console/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.2ms)
  Rendered /home/rubys/git/web-console/lib/web_console/templates/console.js.erb within layouts/javascript (9.2ms)
  Rendered /home/rubys/git/web-console/lib/web_console/templates/main.js.erb within layouts/javascript (0.2ms)
  Rendered /home/rubys/git/web-console/lib/web_console/templates/index.html.erb (15.7ms)

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 included at /home/rubys/.rvm/gems/ruby-2.3.0/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 included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

    
Environment data not found in the schema. To resolve this issue, run: 

    
	bin/rails db:environment:set RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:1259:in `last_stored_environment'
/home/rubys/git/rails/activerecord/lib/active_record/tasks/database_tasks.rb:48:in `check_protected_environments!'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:11:in `block (2 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:375:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:test:load => db:test:purge => db:check_protected_environments
(See full trace by running task with --trace)
rake aborted!
ActiveRecord::PendingMigrationError: 

    
Migrations are pending. To resolve this issue, run:

    
	bin/rails db:migrate RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:578:in `check_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:591:in `load_schema_if_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `block in maintain_test_schema!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:828:in `suppress_messages'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:602:in `method_missing'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `maintain_test_schema!'
/home/rubys/git/rails/railties/lib/rails/test_help.rb:15:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/test_helper.rb:3:in `<top (required)>'
/home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:75:in `plugin_rails_init'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:74:in `block in init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `each'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:123:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:63:in `rake_run'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:32:in `block (3 levels) in <top (required)>'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
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 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
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 included at /home/rubys/.rvm/gems/ruby-2.3.0/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 included at /home/rubys/.rvm/gems/ruby-2.3.0/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:7)
rails aborted!
ActiveRecord::NoEnvironmentInSchemaError: 

    
Environment data not found in the schema. To resolve this issue, run: 

    
	bin/rails db:environment:set RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:1259:in `last_stored_environment'
/home/rubys/git/rails/activerecord/lib/active_record/tasks/database_tasks.rb:48:in `check_protected_environments!'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:11:in `block (2 levels) in <top (required)>'
/home/rubys/git/rails/activerecord/lib/active_record/railties/databases.rake:375:in `block (3 levels) in <top (required)>'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:13:in `block in run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/rake_proxy.rb:10:in `run_rake_task'
/home/rubys/git/rails/railties/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/home/rubys/git/rails/railties/lib/rails/command.rb:20:in `run'
/home/rubys/git/rails/railties/lib/rails/commands.rb:19:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:test:load => db:test:purge => db:check_protected_environments
(See full trace by running task with --trace)
rake aborted!
ActiveRecord::PendingMigrationError: 

    
Migrations are pending. To resolve this issue, run:

    
	bin/rails db:migrate RAILS_ENV=test

    
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:578:in `check_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:591:in `load_schema_if_pending!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `block in maintain_test_schema!'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:828:in `suppress_messages'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:602:in `method_missing'
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:597:in `maintain_test_schema!'
/home/rubys/git/rails/railties/lib/rails/test_help.rb:15:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:268:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:302:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/test_helper.rb:3:in `<top (required)>'
/home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `require'
/home/rubys/git/awdwr/edition4/work-230/depot/test/controllers/carts_controller_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:11:in `block in require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/test_requirer.rb:10:in `require_files'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:75:in `plugin_rails_init'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:74:in `block in init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `each'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:72:in `init_plugins'
/home/rubys/.rvm/gems/ruby-2.3.0@global/gems/minitest-5.8.3/lib/minitest.rb:123:in `run'
/home/rubys/git/rails/railties/lib/rails/test_unit/minitest_plugin.rb:63:in `rake_run'
/home/rubys/git/rails/railties/lib/rails/test_unit/testing.rake:32:in `block (3 levels) in <top (required)>'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
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