Agile Web Development with Rails, Edition 5

26.4 Devise 26.2 HAML

26.3 Pagination

Add in the kaminari gem

edit Gemfile
gem 'kaminari', '~> 1.0'
gem 'activemerchant', '~> 1.58'
gem 'haml', '~> 4.0'

Restart the server.

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
<p id="notice"><%= notice %></p>
 
<h1>Orders</h1>
 
<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Address</th>
      <th>Email</th>
      <th>Pay type</th>
      <th colspan="3"></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
You are being redirected.
get http://localhost:3000/admin

Welcome

It's 2017-03-26 03:45:12 -0400. We have 103 orders.

Show the orders

get /orders

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

26.4 Devise 26.2 HAML