Agile Web Development with Rails, Edition 4

Table of Contents 26.2 HAML

26.3 Pagination

Add in the kaminari gem

edit Gemfile
# source 'https://rubygems.org'
 
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem "activeresource", :path => "/home/rubys/git/activeresource"
gem "activerecord-deprecated_finders", :path => "/home/rubys/git/activerecord-deprecated_finders"
gem "arel", :path => "/home/rubys/git/arel"
gem 'rails', :path => "/home/rubys/git/rails" # '4.0.0.beta'
 
gem 'sqlite3'
group :production do
  gem 'mysql2'
end
 
# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sprockets-rails', :path => "/home/rubys/git/sprockets-rails" # '~> 2.0.0.rc1'
  gem 'sass-rails',   :path => "/home/rubys/git/sass-rails" # '~> 4.0.0.beta'
  gem 'coffee-rails', :path => "/home/rubys/git/coffee-rails" # '~> 4.0.0.beta'
 
  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', platforms: :ruby
 
  gem 'uglifier', '>= 1.0.3'
end
 
gem 'jquery-rails'
 
# Turbolinks makes following links in your web application faster.
# Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
 
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.0.1'
 
# To use ActiveModel has_secure_password
gem 'bcrypt-ruby', '~> 3.0.0'
 
# Use unicorn as the app server
# gem 'unicorn'
 
# Deploy with Capistrano
gem 'capistrano', group: :development
 
# To use debugger
# gem 'debugger'
 
gem 'activemerchant'
gem 'haml', '~> 4.0'
 
gem 'kaminari'
rake environment RAILS_ENV=test db:migrate

Restart the server.

bundle show
Gems included by the bundle:
  * actionmailer (4.0.0.beta)
  * actionpack (4.0.0.beta)
  * active_utils (1.0.5)
  * activemerchant (1.31.0)
  * activemodel (4.0.0.beta)
  * activerecord (4.0.0.beta)
  * activerecord-deprecated_finders (0.0.3 5937f3e)
  * activeresource (4.0.0.beta d5be36d)
  * activesupport (4.0.0.beta)
  * arel (3.0.2.20120819075748 80f11e3)
  * atomic (1.0.1)
  * bcrypt-ruby (3.0.1)
  * builder (3.1.4)
  * bundler (1.3.0.pre.8)
  * capistrano (2.14.2)
  * coffee-rails (4.0.0.beta a30c954)
  * coffee-script (2.2.0)
  * coffee-script-source (1.4.0)
  * erubis (2.7.0)
  * execjs (1.4.0)
  * haml (4.0.0)
  * highline (1.6.15)
  * hike (1.2.1)
  * i18n (0.6.1)
  * jbuilder (1.0.2)
  * jquery-rails (2.2.1)
  * json (1.7.7)
  * kaminari (0.14.1)
  * mail (2.5.3)
  * mime-types (1.21)
  * minitest (4.6.1)
  * 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.5.2)
  * rack-test (0.6.2)
  * rails (4.0.0.beta fb40358)
  * railties (4.0.0.beta)
  * rake (10.0.3)
  * rdoc (3.12.1)
  * sass (3.2.6)
  * sass-rails (4.0.0.beta f6431cf)
  * sprockets (2.8.2)
  * sprockets-rails (2.0.0.rc2 22270f8)
  * sqlite3 (1.3.7)
  * thor (0.17.0)
  * thread_safe (0.1.0)
  * tilt (1.3.3)
  * treetop (1.4.12)
  * turbolinks (1.0.0)
  * tzinfo (0.3.35)
  * uglifier (1.3.0)

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.order('created_at desc').page(params[:page])
  end

Add some navigational aids

edit app/views/orders/index.html.erb
<h1>Listing orders</h1>
 
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Address</th>
      <th>Email</th>
      <th>Pay type</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>
  </thead>
 
  <tbody>
    <% @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 %>
  </tbody>
</table>
 
<br />
 
<%= link_to 'New Order', new_order_path %>
<p><%= 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-24 10:26:27 -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
Customer 90 90 Main Street customer-90@example.com Check Show Edit Destroy
Customer 89 89 Main Street customer-89@example.com Check Show Edit Destroy
Customer 88 88 Main Street customer-88@example.com Check Show Edit Destroy
Customer 87 87 Main Street customer-87@example.com Check Show Edit Destroy
Customer 86 86 Main Street customer-86@example.com Check Show Edit Destroy
Customer 85 85 Main Street customer-85@example.com Check Show Edit Destroy
Customer 84 84 Main Street customer-84@example.com Check Show Edit Destroy
Customer 83 83 Main Street customer-83@example.com Check Show Edit Destroy
Customer 82 82 Main Street customer-82@example.com Check Show Edit Destroy
Customer 81 81 Main Street customer-81@example.com Check Show Edit Destroy
Customer 80 80 Main Street customer-80@example.com Check Show Edit Destroy
Customer 79 79 Main Street customer-79@example.com Check Show Edit Destroy
Customer 78 78 Main Street customer-78@example.com Check Show Edit Destroy
Customer 77 77 Main Street customer-77@example.com Check Show Edit Destroy
Customer 76 76 Main Street customer-76@example.com Check Show Edit Destroy

New Order

Table of Contents 26.2 HAML