Agile Web Development with Rails, Edition 4

Table of Contents 26.2 HAML

26.3 Pagination

Add in the will_paginate gem

edit Gemfile
gem 'activemerchant', '~> 1.31'
gem 'haml', '~> 4.0'
gem 'will_paginate', '~> 3.0'

Restart the server.

bundle show
Gems included by the bundle:
  * actionmailer (3.2.12)
  * actionpack (3.2.12)
  * active_utils (1.0.5)
  * activemerchant (1.31.1)
  * activemodel (3.2.12)
  * activerecord (3.2.12)
  * activeresource (3.2.12)
  * activesupport (3.2.12)
  * arel (3.0.2.20120221150532 d3a8fa9)
  * bcrypt-ruby (3.0.1)
  * builder (3.0.4)
  * bundler (1.3.0)
  * capistrano (2.14.2)
  * coffee-rails (3.2.2 c8f3df3)
  * coffee-script (2.2.0)
  * coffee-script-source (1.5.0)
  * erubis (2.7.0)
  * execjs (1.4.0)
  * haml (4.0.0)
  * highline (1.6.15)
  * hike (1.2.1)
  * i18n (0.6.2)
  * journey (1.0.4.20120614141756 ebe574d)
  * jquery-rails (2.2.1)
  * json (1.7.7)
  * mail (2.5.3)
  * mime-types (1.21)
  * money (5.1.1)
  * multi_json (1.6.1)
  * mysql2 (0.3.11)
  * net-scp (1.1.0)
  * net-sftp (2.1.1)
  * net-ssh (2.6.5)
  * net-ssh-gateway (1.2.0)
  * nokogiri (1.5.6)
  * polyglot (0.3.3)
  * rack (1.4.5)
  * rack-cache (1.2)
  * rack-ssl (1.3.3)
  * rack-test (0.6.2)
  * rails (3.2.12 0761bb0)
  * railties (3.2.12)
  * rake (10.0.3)
  * rdoc (3.12.2)
  * rvm-capistrano (1.2.7)
  * sass (3.2.6)
  * sass-rails (3.2.6 0e2c205)
  * sprockets (2.2.2)
  * sqlite3 (1.3.7)
  * thor (0.17.0)
  * tilt (1.3.3)
  * treetop (1.4.12)
  * tzinfo (0.3.35)
  * uglifier (1.3.0)
  * will_paginate (3.0.4)

Load in a few orders

edit script/load_orders.rb
Order.transaction do
  (1..100).each do |i|
    Order.create(name: "Customer #{i}", address: "#{i} Main Street",
      email: "customer-#{i}@example.com", pay_type: "Check")
  end
end
rails runner script/load_orders.rb

Modify the controller to do pagination

edit app/controllers/orders_controller.rb
  def index
    @orders = Order.paginate page: params[:page], order: 'created_at desc',
      per_page: 10
 
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @orders }
    end
  end

Add some navigational aids

edit app/views/orders/index.html.erb
<h1>Listing orders</h1>
 
<table>
  <tr>
    <th>Name</th>
    <th>Address</th>
    <th>Email</th>
    <th>Pay type</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>
 
<% @orders.each do |order| %>
  <tr>
    <td><%= order.name %></td>
    <td><%= order.address %></td>
    <td><%= order.email %></td>
    <td><%= order.pay_type %></td>
    <td><%= link_to 'Show', order %></td>
    <td><%= link_to 'Edit', edit_order_path(order) %></td>
    <td><%= link_to 'Destroy', order, method: :delete,
              data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>
 
<br />
 
<%= link_to 'New Order', new_order_path %>
<p><%= will_paginate @orders %></p>

Do a login

get /login
Please Log In
post /login?locale=en
You are being redirected.
get http://localhost:3000/admin?locale=en

Welcome

It's 2013-02-25 20:20:58 -0500 We have 102 orders.

Show the orders

get /orders

Listing orders

Name Address Email Pay type
Customer 100 100 Main Street customer-100@example.com Check Show Edit Destroy
Customer 99 99 Main Street customer-99@example.com Check Show Edit Destroy
Customer 98 98 Main Street customer-98@example.com Check Show Edit Destroy
Customer 97 97 Main Street customer-97@example.com Check Show Edit Destroy
Customer 96 96 Main Street customer-96@example.com Check Show Edit Destroy
Customer 95 95 Main Street customer-95@example.com Check Show Edit Destroy
Customer 94 94 Main Street customer-94@example.com Check Show Edit Destroy
Customer 93 93 Main Street customer-93@example.com Check Show Edit Destroy
Customer 92 92 Main Street customer-92@example.com Check Show Edit Destroy
Customer 91 91 Main Street customer-91@example.com Check Show Edit Destroy

New Order

Table of Contents 26.2 HAML