Agile Web Development with Rails, Edition 4

Agile Web Development with Rails, Edition 4

12.4 Playtime 12.2 Iteration G2: Atom Feeds

12.3 Iteration G3: Pagination

Add in the will_paginate gem

edit Gemfile
# source 'http://rubygems.org'
 
gem 'rails', :path => "/home/rubys/git/rails" # '3.0.0.beta3'
 
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
 
gem 'sqlite3-ruby', :require => 'sqlite3'
 
# Use unicorn as the web server
# gem 'unicorn'
 
# Deploy with Capistrano
# gem 'capistrano'
 
# To use debugger
# gem 'ruby-debug'
 
# Bundle the extra gems:
# gem 'bj'
# gem 'nokogiri', '1.4.1'
# gem 'sqlite3-ruby', :require => 'sqlite3'
# gem 'aws-s3', :require => 'aws/s3'
 
gem 'will_paginate', '>= 3.0.pre'
 
# Bundle gems for certain environments:
# gem 'rspec', :group => :test
# group :test do
#   gem 'webrat'
# end

Restart the server.

bundle show
Gems included by the bundle:
  * abstract (1.0.0)
  * actionmailer (3.0.0.beta3)
  * actionpack (3.0.0.beta3)
  * activemodel (3.0.0.beta3)
  * activerecord (3.0.0.beta3)
  * activeresource (3.0.0.beta3)
  * activesupport (3.0.0.beta3)
  * arel (0.3.3)
  * builder (2.1.2)
  * bundler (0.9.24)
  * erubis (2.6.5)
  * i18n (0.4.1)
  * mail (2.2.1)
  * mime-types (1.16)
  * polyglot (0.3.1)
  * rack (1.1.0)
  * rack-mount (0.6.3)
  * rack-test (0.5.4)
  * rails (3.0.0.beta3 509f3d)
  * railties (3.0.0.beta3)
  * rake (0.8.7)
  * sqlite3-ruby (1.2.5)
  * thor (0.13.6)
  * treetop (1.4.8)
  * tzinfo (0.3.22)
  * will_paginate (3.0.pre)

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
DEPRECATION WARNING: railtie_name is deprecated and has no effect. (called from <class:Railtie> at /home/rubys/.rvm/gems/ruby-1.9.3-r28190/gems/will_paginate-3.0.pre/lib/will_paginate/railtie.rb:6)

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.xml  { render :xml => @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, :confirm => 'Are you sure?',
              :method => :delete %></td>
  </tr>
<% end %>
</table>
 
<br />
 
<%= link_to 'New Order', new_order_path %>
<p><%= will_paginate @orders %></p>

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

12.4 Playtime 12.2 Iteration G2: Atom Feeds