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
<p id="notice"><%= notice %></p>
 
<h2>Your Pragmatic Cart</h2>
<ul>    
  <% @cart.line_items.each do |item| %>
    <li><%= item.quantity %> &times; <%= item.product.title %></li>
  <% end %>
</ul>
 
<%= button_to 'Empty cart', @cart, method: :delete,
    data: { confirm: 'Are you sure?' } %>

Clear session and change flash notice when cart is destroyed.

edit app/controllers/carts_controller.rb
  def destroy
    @cart.destroy if @cart.id == session[:cart_id]
    session[:cart_id] = nil
    respond_to do |format|
      format.html { redirect_to store_url,
        notice: 'Your cart is currently empty' }
      format.json { head :no_content }
    end
  end

Try it out.

get /carts/1

Your Pragmatic Cart

  • 3 × Programming Ruby 1.9 & 2.0
  • 1 × CoffeeScript
post /carts/1
You are being redirected.
get http://localhost:3000/

Your cart is currently empty

Your Pragmatic Catalog

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$36.00
Ruby

Programming Ruby 1.9 & 2.0

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.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95

Remove scaffolding generated flash notice for line item create.

edit app/controllers/line_items_controller.rb
  def create
    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.json { render :show,
          status: :created, location: @line_item }
      else
        format.html { render :new }
        format.json { render json: @line_item.errors,
          status: :unprocessable_entity }
      end
    end
  end

Update the view to add totals.

edit app/views/carts/show.html.erb
<p id="notice"><%= notice %></p>
 
<h2>Your Cart</h2>
<table>
  <% @cart.line_items.each do |item| %>
    <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,
    data: { 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 app/assets/stylesheets/carts.scss
// Place all the styles related to the Carts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
 
.carts {
  .item_price, .total_line {
    text-align: right;
  }
 
  .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

Cs

CoffeeScript

CoffeeScript is JavaScript done right. It provides all of JavaScript's functionality wrapped in a cleaner, more succinct syntax. In the first book on this exciting new language, CoffeeScript guru Trevor Burnham shows you how to hold onto all the power and flexibility of JavaScript while writing clearer, cleaner, and safer code.

$36.00
Ruby

Programming Ruby 1.9 & 2.0

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.95
Rtp

Rails Test Prescriptions

Rails Test Prescriptions is a comprehensive guide to testing Rails applications, covering Test-Driven Development from both a theoretical perspective (why to test) and from a practical perspective (how to test effectively). It covers the core Rails testing tools and procedures for Rails 2 and Rails 3, and introduces popular add-ons, including Cucumber, Shoulda, Machinist, Mocha, and Rcov.

$34.95
post /line_items?product_id=2
You are being redirected.
get http://localhost:3000/carts/2

Your Cart

CoffeeScript $36.00
Total $36.00

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

CoffeeScript $72.00
Total $72.00
post /line_items?product_id=3
You are being redirected.
get http://localhost:3000/carts/2

Your Cart

CoffeeScript $72.00
Programming Ruby 1.9 & 2.0 $49.95
Total $121.95

10.4 Playtime 10.2 Iteration E2: Handling Errors