Your SlideShare is downloading. ×
0
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Agile Web Dev. With Rails Ch 7 - Cart Creation
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Agile Web Dev. With Rails Ch 7 - Cart Creation

4,203

Published on

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

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,203
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
25
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • - current_cart available to all controllers‘create’ an ActiveRecord method that creates & 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 <form> and <div> 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

    • 1. Cart Creation<br />
    • 2. What we’ll cover<br />Using Sessions<br />Public, Private, and Protected Methods in Ruby<br />Relationships between models<br />Active Record Callbacks &amp; Validation<br />Adding a button to a page<br />More Functional Testing<br />
    • 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. 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. 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. Public, Private, &amp; Protected Methods<br />Different from Java &amp; 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 &amp; objects from derived classes<br />Inheritance doesn’t determine method visibility<br />Private &amp; protected methods visible from subclasses<br />
    • 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. 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. Add Relationship Specifiers<br />class Cart &lt; ActiveRecord::Base<br />has_many :line_items, :dependent =&gt; :destroy<br />end<br />class LineItem &lt; ActiveRecord::Base<br />belongs_to :product<br />belongs_to :cart<br />end<br />class Product &lt; ActiveRecord::Base<br />has_many :line_items<br />end<br />
    • 10. What if we destroy a cart?<br />Line_items dependent on cart<br />:dependent =&gt; :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. What if we destroy a product?<br />Make sure it isn’t contained in a cart<br />
    • 12. ActiveRecord Callbacks &amp; Validations<br />Callbacks invoked before &amp; after operations<br />For example save, delete, create, validate<br />Can view unformatted data in ‘before’ callbacks<br />Dates &amp; Currency<br />Returning the actual value ‘false’ stops the operation<br />Add a handler or define as a method<br />
    • 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 />&lt;%= button_to ‘Add to Cart’, line_items_path(:product_id =&gt; product) %&gt;<br />
    • 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. Modify line_item create (cont.)<br />app/controllers/line_items_controller.rb<br />
    • 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. Try it out<br />http://localhost:3000/<br />Cart scaffolding didn’t show any attributes of what was just added<br />
    • 18. Modify the view<br />app/views/cart/show.html.erb<br />&lt;h2&gt;Your Pragmatic Cart&lt;/h2&gt;<br />&lt;ul&gt;<br /> &lt;% for item in @cart.line_items %&gt;<br /> &lt;li&gt;&lt;%= item.product.title %&gt;&lt;/li&gt;<br /> &lt;% end %&gt;<br />&lt;/ul&gt;<br />
    • 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. 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 &gt; 5<br />

    ×