Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

10.4 Playtime 10.2 Iteration E2: Handling Errors

10.3 Iteration E3: Finishing the Cart

Add empty cart button, remove flash for line item create, add totals to view.

Add button to the view.

edit app/views/carts/show.html.erb
<h2>Your Pragmatic Cart</h2>
<ul>    
  <% for item in @cart.line_items %>
    <li><%= item.quantity %> &times; <%= item.product.title %></li>
  <% end %>
</ul>
 
<%= button_to 'Empty cart', @cart, :method => :delete,
    :confirm => 'Are you sure?' %>

Clear session and add flash notice when cart is destroyed.

edit app/controllers/carts_controller.rb
  def destroy
    @cart = Cart.find(params[:id])
    @cart.destroy
    session[:cart_id] = nil
 
    respond_to do |format|
      format.html { redirect_to(store_url,
        :notice => 'Your cart is currently empty') }
      format.xml  { head :ok }
    end
  end

Try it out.

get /carts/1

Your Pragmatic Cart

  • 3 × Programming Ruby 1.9
  • 1 × Web Design for Developers
post /carts/1
You are being redirected.
get http://localhost:3000/

Your cart is currently empty

Your Pragmatic Catalog

Debug

Debug It!

Professional programmers develop a knack of unerringly zeroing in on the root cause of a bug. They can do that because they've written a lot of buggy code and then gained experience fixing it. This book captures all this experience -- use it, and you'll find you write fewer bugs, and the ones you do write will become easier to hunt down.

$34.95
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.50
Wd4d

Web Design for Developers

Web Design for Developers will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

$42.95
pub depot_h

Remove scaffolding generated flash notice for line item create.

edit app/controllers/line_items_controller.rb
  def create
    @cart = find_or_create_cart
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product.id)
 
    respond_to do |format|
      if @line_item.save
        format.html { redirect_to(@line_item.cart) }
        format.xml  { render :xml => @line_item,
          :status => :created, :location => @line_item }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @line_item.errors,
          :status => :unprocessable_entity }
      end
    end
  end

Update the view to add totals.

edit app/views/carts/show.html.erb
<div class="cart_title">Your Cart</div>
<table>
  <% for item in @cart.line_items %>
    <tr>
      <td><%= item.quantity %>&times;</td>
      <td><%= item.product.title %></td>
      <td class="item_price"><%= number_to_currency(item.total_price) %></td>
    </tr>
  <% end %>
 
  <tr class="total_line">
    <td colspan="2">Total</td>
    <td class="total_cell"><%= number_to_currency(@cart.total_price) %></td>
  </tr>
 
</table>
 
<%= button_to 'Empty cart', @cart, :method => :delete,
    :confirm => 'Are you sure?' %>

Add a method to compute the total price of a single line item.

edit app/models/line_item.rb
 
  def total_price
    product.price * quantity
  end

Add a method to compute the total price of the items in the cart.

edit app/models/cart.rb
  def total_price
    line_items.to_a.sum { |item| item.total_price }
  end

Add some style.

edit public/stylesheets/depot.css
/* Styles for the cart in the main page */
 
#store .cart_title {
  font: 120% bold;
}
 
#store .item_price, #store .total_line {
  text-align: right;
}
 
#store .total_line .total_cell {
  font-weight: bold;
  border-top: 1px solid #595;
}

Add a product to the cart, and see the total.

get /

Your Pragmatic Catalog

Debug

Debug It!

Professional programmers develop a knack of unerringly zeroing in on the root cause of a bug. They can do that because they've written a lot of buggy code and then gained experience fixing it. This book captures all this experience -- use it, and you'll find you write fewer bugs, and the ones you do write will become easier to hunt down.

$34.95
Ruby

Programming Ruby 1.9

Ruby is the fastest growing and most exciting dynamic language out there. If you need to get working programs delivered fast, you should add Ruby to your toolbox.

$49.50
Wd4d

Web Design for Developers

Web Design for Developers will show you how to make your web-based application look professionally designed. We'll help you learn how to pick the right colors and fonts, avoid costly interface and accessibility mistakes -- your application will really come alive. We'll also walk you through some common Photoshop and CSS techniques and work through a web site redesign, taking a new design from concept all the way to implementation.

$42.95
post /line_items?product_id=2
You are being redirected.
get http://localhost:3000/carts/2
Your Cart
Web Design for Developers $42.95
Total $42.95

Add a few more products, and watch the totals climb!

post /line_items?product_id=2
You are being redirected.
get http://localhost:3000/carts/2
Your Cart
Web Design for Developers $85.90
Total $85.90
post /line_items?product_id=3
You are being redirected.
get http://localhost:3000/carts/2
Your Cart
Web Design for Developers $85.90
Programming Ruby 1.9 $49.50
Total $135.40

10.4 Playtime 10.2 Iteration E2: Handling Errors