The Depot Application

The Depot Application

8.3 Iteration C2: Creating a Smarter Cart 8.1 Sessions

8.2 Iteration C1: Creating a Cart

edit app/models/cart.rb
class Cart
  attr_reader :items   # <wtf linkend="wtf.attr.accessor">attr_reader</wtf>
  
  def initialize
    @items = []
  end
  
  def add_product(product)
    @items << product
  end
end
edit app/views/store/index.html.erb
    <%= button_to 'Add to Cart', :action => 'add_to_cart', :id => product %>
edit app/controllers/store_controller.rb
  def add_to_cart
    product = Product.find(params[:id]) # <label id="code.depot.f.find"/>
    @cart = find_cart                   # <label id="code.depot.f.find2"/>
    @cart.add_product(product)          # <label id="code.depot.f.add"/>
  end
get /store/add_to_cart/2

Template is missing

Missing template store/add_to_cart with {:formats=>[:html], :handlers=>[:builder, :rjs, :rhtml, :erb, :rxml], :locale=>[:en, :en]} in view paths "/home/rubys/git/awdwr/work-188/depot/app/views"

edit app/views/store/add_to_cart.html.erb
<h2>Your Pragmatic Cart</h2>
<ul>
  <% for item in @cart.items %>
    <li><%=h item.title %></li>
  <% end %>
</ul>
get /store/add_to_cart/2

Your Pragmatic Cart

  • Pragmatic Project Automation
get /store/add_to_cart/3

Your Pragmatic Cart

  • Pragmatic Project Automation
  • Pragmatic Version Control

8.3 Iteration C2: Creating a Smarter Cart 8.1 Sessions