Agile Web Development with Rails, Edition 4

12.4 Playtime 12.2 Iteration G2: Atom Feeds

12.3 Iteration G3: Downloading an eBook

demonstrate streaming with ActionController::Live

Switch to puma as a server

edit Gemfile
gem 'puma'

Restart the server.

add a route for downloading a product

edit config/routes.rb

mock streaming implementation for download

edit app/controllers/products_controller.rb
  include ActionController::Live
  def download
    response.headers['Content-Type'] = 'text/plain'
    40.times do |i|
      response.stream.write "Line #{i}\n\n"
      sleep 0.10
    end
    response.stream.write "Fini.\n"
  ensure
    response.stream.close
  end

add order to the session

edit app/controllers/orders_controller.rb
  def create
    @order = Order.new(order_params)
    @order.add_line_items_from_cart(@cart)
 
    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        session[:order_id] = @order.id
        format.html { redirect_to store_url, notice: 
          'Thank you for your order.' }
        format.json { render :show, status: :created,
          location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors,
          status: :unprocessable_entity }
      end
    end
  end

render order in the side bar

edit app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
  <title>Pragprog Books Online Store</title>
  <%= stylesheet_link_tag    "application", media: "all",
    "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
</head>
<body class="<%= controller.controller_name %>">
  <div id="banner">
    <%= image_tag("logo.png") %>
    <%= @page_title || "Pragmatic Bookshelf" %>
  </div>
  <div id="columns">
    <div id="side">
      <% if @cart %>
        <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
          <%= render @cart %>
        <% end %>
      <% end %>
 
      <%= render Order.find(session[:order_id]) if session[:order_id] -%>
 
      <ul>
        <li><a href="http://www....">Home</a></li>
        <li><a href="http://www..../faq">Questions</a></li>
        <li><a href="http://www..../news">News</a></li>
        <li><a href="http://www..../contact">Contact</a></li>
      </ul>
    </div>
    <div id="main">
      <%= yield %>
    </div>
  </div>
</body>
</html>

implement order partial

edit app/views/orders/_order.html.erb
<div id="order">
<h2>Your Downloads</h2>
<table data-no-turbolink>
  <% order.line_items.each do |item| %>
  <tr>
    <td><%= link_to item.product.title, download_product_path(item.product) %></td>
  </tr>
  <% end %>
</table>
</div>

css tweaks

edit app/assets/stylesheets/application.css.scss
  #side {
    float: left;
    padding: 1em 2em;
    width: 13em;
    background: #141;
 
    form, div {
      display: inline;
    }  
 
    input {
      font-size: small;
    }
 
    #cart, #order {
      font-size: smaller;
      color:     white;
 
      a, a:hover {
        color: white;
        background-color: #141;
      }
 
      table {
        border-top:    1px dotted #595;
        border-bottom: 1px dotted #595;
        margin-bottom: 10px;
      }
    }
 
    ul {
      padding: 0;
 
      li {
        list-style: none;
 
        a {
          color: #bfb;
          font-size: small;
        }
      }
    }
  }

place an order

get /

Getting started

Here’s how to get rolling:

  1. Use bin/rails generate to create your models and controllers

    To see all available options, run it without parameters.

  2. Set up a root route to replace this page

    You're seeing this page because you're running in development mode and you haven't set a root route yet.

    Routes are set up in config/routes.rb.

  3. Configure your database

    If you're not using SQLite (the default), edit config/database.yml with your username and password.

get /orders/new

NameError in OrdersController#new

undefined local variable or method `store_url' for #<OrdersController:0x007fb90c3d4498> Did you mean? store_index_url

Extracted source (around line #26):
24
25
26
27
28
29
              
#START_HIGHLIGHT
if @cart.line_items.empty?
redirect_to store_url, notice: "Your cart is empty"
return
end

Rails.root: /home/rubys/git/awdwr/edition4/work-230/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Response

Headers:

None

click download

get /products/2/download
Line 0

      
Line 1

      
Line 2

      
Line 3

      
Line 4

      
Line 5

      
Line 6

      
Line 7

      
Line 8

      
Line 9

      
Line 10

      
Line 11

      
Line 12

      
Line 13

      
Line 14

      
Line 15

      
Line 16

      
Line 17

      
Line 18

      
Line 19

      
Line 20

      
Line 21

      
Line 22

      
Line 23

      
Line 24

      
Line 25

      
Line 26

      
Line 27

      
Line 28

      
Line 29

      
Line 30

      
Line 31

      
Line 32

      
Line 33

      
Line 34

      
Line 35

      
Line 36

      
Line 37

      
Line 38

      
Line 39

      
Fini.

Switch back to WEBRick

edit Gemfile
# gem 'puma'

Restart the server.

12.4 Playtime 12.2 Iteration G2: Atom Feeds