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:216: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_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
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.
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.
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.
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.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 (8.5ms)
Started GET "/carts/wibble" for 127.0.0.1 at 2016-06-13 15:40:54 -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/store/index
Completed 302 Found in 3ms (ActiveRecord: 0.4ms)
Started GET "/store/index" for 127.0.0.1 at 2016-06-13 15:40:54 -0400
Processing by StoreController#index as HTML
Product Load (0.2ms) SELECT "products".* FROM "products" ORDER BY "products"."title" ASC
Rendered store/index.html.erb within layouts/application (5.9ms)
Completed 200 OK in 12ms (Views: 9.3ms | 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
rake aborted!
ActiveRecord::PendingMigrationError: Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=test' to resolve this issue.
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:383:in `check_pending!'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/test_helper.rb:6:in `<class:TestCase>'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/test_helper.rb:5:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/controllers/carts_controller_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `block (3 levels) in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `block (2 levels) in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:72:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:72:in `block in define'
/home/rubys/.rvm/gems/ruby-2.2.5/gems/rake-11.2.1/exe/rake:27:in `<top (required)>'
/home/rubys/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:controllers
(See full trace by running task with --trace)
Run options: --seed 22208
# Running tests:
Finished tests in 0.000789s, 0.0000 tests/s, 0.0000 assertions/s.
0 tests, 0 assertions, 0 failures, 0 errors, 0 skips
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, 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
rake aborted!
ActiveRecord::PendingMigrationError: Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=test' to resolve this issue.
/home/rubys/git/rails/activerecord/lib/active_record/migration.rb:383:in `check_pending!'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/test_helper.rb:6:in `<class:TestCase>'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/test_helper.rb:5:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/awdwr/edition4/work-225-40/depot/test/controllers/carts_controller_test.rb:1:in `<top (required)>'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `block in require'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:214:in `load_dependency'
/home/rubys/git/rails/activesupport/lib/active_support/dependencies.rb:229:in `require'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `block (3 levels) in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:73:in `block (2 levels) in define'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:72:in `each'
/home/rubys/git/rails/railties/lib/rails/test_unit/sub_test_task.rb:72:in `block in define'
/home/rubys/.rvm/gems/ruby-2.2.5/gems/rake-11.2.1/exe/rake:27:in `<top (required)>'
/home/rubys/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `eval'
/home/rubys/.rvm/gems/ruby-2.2.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => test:controllers
(See full trace by running task with --trace)
Run options: --seed 62963
# Running tests:
Finished tests in 0.000910s, 0.0000 tests/s, 0.0000 assertions/s.
0 tests, 0 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