Agile Web Development with Rails, Edition 5

26.4 Devise 26.2 HAML

26.3 Pagination

Add in the kaminari gem

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

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 2016-05-06 21:46:59 -0400 We have 103 orders.

Show the orders

get /orders

HTTP Response Code: 500

ArgumentError in Orders#index

Showing /home/rubys/.rvm/gems/ruby-2.2.4/gems/kaminari-0.16.3/app/views/kaminari/_paginator.html.erb where line #14 raised:

Generating a URL from non sanitized request parameters is insecure!
Extracted source (around line #36):
34
35
36
37
              
<%= link_to 'New Order', new_order_path %>
<!-- START_HIGHLIGHT -->
<p><%= paginate @orders %></p>
<!-- END_HIGHLIGHT -->

Trace of template inclusion: app/views/orders/index.html.erb

Rails.root: /home/rubys/git/awdwr/edition4/work-224-500beta3/depot

Application Trace | Framework Trace | Full Trace

Request

Parameters:

None

Response

Headers:

None

26.4 Devise 26.2 HAML