Your SlideShare is downloading. ×
0
Cart Creation<br />
What we’ll cover<br />Using Sessions<br />Public, Private, and Protected Methods in Ruby<br />Relationships between models...
Generate a shopping cart<br />rails generate scaffold cart<br />No foreign keys present<br />Only one of the tables has a ...
Rails Sessions<br />By default, session info stored in a browser session cookie (4k limit)<br />Database or memcached also...
Associate the cart with a session<br />app/controllers/application_controller.rb<br />private<br />defcurrent_cart<br />Ca...
Public, Private, & Protected Methods<br />Different from Java & C#<br />Public methods can be invoked by anyone <br />Priv...
Storing items in the cart<br />rails generate scaffold line_itemproduct_id:integercart_id:integer<br />By convention, sing...
ActiveRecord relationships<br />One to One<br />has_one :contained<br />belongs_to :container<br />One to Many<br />has_ma...
Add Relationship Specifiers<br />class Cart < ActiveRecord::Base<br />has_many :line_items, :dependent => :destroy<br />en...
What if we destroy a cart?<br />Line_items dependent on cart<br />:dependent => :destroy indicates that line_items are des...
What if we destroy a product?<br />Make sure it isn’t contained in a cart<br />
ActiveRecord Callbacks & Validations<br />Callbacks invoked before & after operations<br />For example save, delete, creat...
Add a button<br />Won’t be creating a new controller or a new action<br />Creating an item in the cart, so use line_item c...
Modify line_item create<br />By default, create parameter is a serialized line_item object - params[:line_item]<br />Creat...
Modify line_item create (cont.)<br />app/controllers/line_items_controller.rb<br />
Update the functional test<br />test/functional/line_items_controller_test.rb<br />assigns() accesses instance variables i...
Try it out<br />http://localhost:3000/<br />Cart scaffolding didn’t show any attributes of what was just added<br />
Modify the view<br />app/views/cart/show.html.erb<br /><h2>Your Pragmatic Cart</h2><br /><ul><br />   <% for item in @cart...
Try it out (again)<br />http://localhost:3000/<br />Cart now shows what is contained<br />Need to show each item only once...
Homework<br />Change the view so that clicking on a book’s image will also add the product to the cart<br />Add a new sess...
Upcoming SlideShare
Loading in...5
×

Agile Web Dev. With Rails Ch 7 - Cart Creation

4,263

Published on

Chapter 7 of "Agile Web Development with Rails" 4th Ed.

Presented at ATLRUG-Emerald City
Saturday, December 18, 2010

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,263
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
25
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • - current_cart available to all controllers‘create’ an ActiveRecord method that creates &amp; stores in the databaseas an aside, ‘private’ will look wrong to those from a Java or C# background
  • Other relationships are available - Many to Many - Relationships through a 3rd model
  • Can do things like li = LineItem.find(…) puts “This is for #{li.product.title}”Cart can reference its collection cart.line_items.count
  • Use an ActiveRecord callback to perform validation
  • - Unformatted data in validation callback
  • Links use HTTP GET, Buttons use HTTP POSTbutton_to uses &lt;form&gt; and &lt;div&gt; elements which drop down to another line unless made inline with CSS
  • ActiveRecord ‘build’ allows you to add an object to the collection with references set
  • - Updatetest to reflect changes in the controller- In the “real world”, we would update the test first- rake test:functionals
  • Transcript of "Agile Web Dev. With Rails Ch 7 - Cart Creation"

    1. 1. Cart Creation<br />
    2. 2. What we’ll cover<br />Using Sessions<br />Public, Private, and Protected Methods in Ruby<br />Relationships between models<br />Active Record Callbacks & Validation<br />Adding a button to a page<br />More Functional Testing<br />
    3. 3. Generate a shopping cart<br />rails generate scaffold cart<br />No foreign keys present<br />Only one of the tables has a foreign key<br />Need to recover the cart with every request<br />
    4. 4. Rails Sessions<br />By default, session info stored in a browser session cookie (4k limit)<br />Database or memcached also available<br />Don’t store data that can easily be outdated<br />Serialized ActiveRecordobjects<br />Cryptographically signed but unencrypted<br />Don’t store critical information solely in the session<br />
    5. 5. Associate the cart with a session<br />app/controllers/application_controller.rb<br />private<br />defcurrent_cart<br />Cart.find(session[:cart_id])<br />rescue ActiveRecord::RecordNotFound<br /> cart = Cart.create<br /> session[:cart_id] = cart.id<br /> cart<br /> end<br />
    6. 6. Public, Private, & Protected Methods<br />Different from Java & C#<br />Public methods can be invoked by anyone <br />Private methods accessible by an object from a derived class<br />Protected methods accessible by other objects & objects from derived classes<br />Inheritance doesn’t determine method visibility<br />Private & protected methods visible from subclasses<br />
    7. 7. Storing items in the cart<br />rails generate scaffold line_itemproduct_id:integercart_id:integer<br />By convention, singular of table name appended with “_id”<br />Need to establish relationship in both the database and the model<br />Model declarations add ability to build and navigate the relationship<br />
    8. 8. ActiveRecord relationships<br />One to One<br />has_one :contained<br />belongs_to :container<br />One to Many<br />has_many :contained<br />belongs_to :container<br />Table with foreign keys has the ‘belongs_to’<br />
    9. 9. Add Relationship Specifiers<br />class Cart < ActiveRecord::Base<br />has_many :line_items, :dependent => :destroy<br />end<br />class LineItem < ActiveRecord::Base<br />belongs_to :product<br />belongs_to :cart<br />end<br />class Product < ActiveRecord::Base<br />has_many :line_items<br />end<br />
    10. 10. What if we destroy a cart?<br />Line_items dependent on cart<br />:dependent => :destroy indicates that line_items are destroyed along with the cart<br />destroy involves ActiveRecord validation logic, delete only removes from the database<br />
    11. 11. What if we destroy a product?<br />Make sure it isn’t contained in a cart<br />
    12. 12. ActiveRecord Callbacks & Validations<br />Callbacks invoked before & after operations<br />For example save, delete, create, validate<br />Can view unformatted data in ‘before’ callbacks<br />Dates & Currency<br />Returning the actual value ‘false’ stops the operation<br />Add a handler or define as a method<br />
    13. 13. Add a button<br />Won’t be creating a new controller or a new action<br />Creating an item in the cart, so use line_item controller<br />Looking at generated code in line_item_controller.rb, we’ll use the ‘Create’ action<br />‘Create’ action called using HTTP Post<br />button_to‘button label’, ‘URL Path’, ‘HTML options’<br />Add ‘_path’ to controller to get URL path (i.e. line_item_path)<br />Pass in hash values to URL path as parameters<br />id extracted from ActiveRecord objects passed in<br />app/views/store/index.html.erb<br /><%= button_to ‘Add to Cart’, line_items_path(:product_id => product) %><br />
    14. 14. Modify line_item create<br />By default, create parameter is a serialized line_item object - params[:line_item]<br />Create a line_item based on the product id and add it to the cart<br />
    15. 15. Modify line_item create (cont.)<br />app/controllers/line_items_controller.rb<br />
    16. 16. Update the functional test<br />test/functional/line_items_controller_test.rb<br />assigns() accesses instance variables in the controller being tested<br />
    17. 17. Try it out<br />http://localhost:3000/<br />Cart scaffolding didn’t show any attributes of what was just added<br />
    18. 18. Modify the view<br />app/views/cart/show.html.erb<br /><h2>Your Pragmatic Cart</h2><br /><ul><br /> <% for item in @cart.line_items %><br /> <li><%= item.product.title %></li><br /> <% end %><br /></ul><br />
    19. 19. Try it out (again)<br />http://localhost:3000/<br />Cart now shows what is contained<br />Need to show each item only once (next chapter)<br />
    20. 20. Homework<br />Change the view so that clicking on a book’s image will also add the product to the cart<br />Add a new session variable to record how many times the user accessed the store controller’s index action<br />Add the counter to the template and display it on top of the catalog page<br />Reset the counter to zero when adding to the cart<br />Change template to display counter when > 5<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×