Smarter cart

1,567 views

Published on

Chapter 10 1/8/2011

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,567
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Smarter cart

  1. 1. Creating a Smarter Cart Jason Noble http://jasonnoble.org
  2. 2. Adding a count to our cart rails generate migration add_quantity_to_line_itemquantity:integer Rails recognizes add_XXX_to_TABLE Rails recognizes remove_XXX_to_TABLE Modify the migration to specify a default value add_column :line_items, :quantity, :integer, :default => 1
  3. 3. Add Product Method app/models/cart.rb
  4. 4. Update Line Items Controller Change create method
  5. 5. Modify Cart Show View Add the quantity to the cart view app/views/carts/show.html.erb
  6. 6. Still have duplicates
  7. 7. Write a migration to fix existing carts rails generate migration combine_items_in_cart
  8. 8. Add an undo option Modify the migration self.down method rake db:rollback
  9. 9. Handling Errors
  10. 10. Sending messages to the user Rails has a structure called flash flash is a Hash that you can you can store stuff in as you process a request Contents of the flash are available to the next request and then automatically deleted Store error and debug messages in the flash to help users understand what’s happening flash is stored in the user’s session to make it available between requests
  11. 11. Add a message to the flash app/controllers/carts_controller.rb
  12. 12. Verify it works http://localhost:3000/carts/wibble
  13. 13. Emptying your cart app/views/carts/show.html.erb
  14. 14. Emptying your cart (cont.) app/controllers/carts_controller.rb
  15. 15. Run your tests often rake test test/functional/carts_controller_test.rb
  16. 16. Better Cart View app/views/carts/show.html.erb
  17. 17. Add total_price methods app/models/line_item.rb app/models/cart.rb
  18. 18. Make it prettier public/stylesheets/depot.css
  19. 19. What we covered Adding/Removing a column to an existing table with a default value Migrating existing table data into new format Providing flash notices to customer Using the logger to log events Deleted a record Adjusted the way the cart is viewed, using CSS
  20. 20. Homework Create a migration that stores the product price in the line item table. Modify add_product method to capture the price. Add unit tests which add unique products and duplicate products. Use the flash functionality to show helpful messages Add the ability to delete individual line items from the cart. Bonus points to decrease quantity by 1.

×