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 'https://rubygems.org'
gem 'rails', :path => "/home/rubys/git/rails" # '4.0.0.beta'
gem "arel", :path => "/home/rubys/git/arel"
gem "journey", :path => "/home/rubys/git/journey"
# Bundle edge Rails instead:
# gem 'rails', :git => 'https://github.com/rails/rails.git'
gem 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
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'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the web server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano', :group => :development
# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
gem 'will_paginate', '~> 3.0'
Restart the server.
bundle show
Gems included by the bundle:
* actionmailer (4.0.0.beta)
* actionpack (4.0.0.beta)
* activemodel (4.0.0.beta)
* activerecord (4.0.0.beta)
* activeresource (4.0.0.beta)
* activesupport (4.0.0.beta)
* arel (3.0.0.20120112113618 eebf5d7)
* builder (3.0.0)
* bundler (1.0.21)
* coffee-rails (3.2.0 6b4034a)
* coffee-script (2.2.0)
* coffee-script-source (1.2.0)
* erubis (2.7.0)
* execjs (1.3.0)
* hike (1.2.1)
* i18n (0.6.0)
* journey (1.0.1.20120208102204 38dd4ee)
* jquery-rails (2.0.0)
* json (1.6.5)
* mail (2.4.1)
* mime-types (1.17.2)
* multi_json (1.0.4)
* polyglot (0.3.3)
* rack (1.4.1)
* rack-cache (1.1)
* rack-ssl (1.3.2)
* rack-test (0.6.1)
* rails (4.0.0.beta 815e445)
* railties (4.0.0.beta)
* rake (0.9.2.2)
* rdoc (3.12)
* sass (3.1.15)
* sass-rails (4.0.0.beta 922b199)
* sprockets (2.2.0)
* sqlite3 (1.3.5)
* thor (0.14.6)
* tilt (1.3.3)
* treetop (1.4.10)
* tzinfo (0.3.31)
* uglifier (1.2.3)
* will_paginate (3.0.3)
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>
<%= content_tag_for(:tr, @orders) do |order| %>
<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>
<% end %>
</table>
<br />
<%= link_to 'New Order', new_order_path %>
<p><%= will_paginate @orders %></p>
Show the orders
get /orders
Pragmatic Bookshelf
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