Agile Web Development with Rails, Edition 4
12.4 Playtime
12.2 Iteration G2: Atom Feeds
12.3 Iteration G3: 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 "journey", :path => "/home/rubys/git/journey"
gem 'rails', :path => "/home/rubys/git/rails" # '4.0.0.beta'
gem 'sqlite3'
# 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'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano', group: :development
# To use debugger
# gem 'debugger'
gem 'kaminari'
rake environment RAILS_ENV=test db:migrate
== CreateOrders: migrating ===================================================
-- create_table(:orders)
-> 0.0575s
== CreateOrders: migrated (0.0576s) ==========================================
== AddOrderIdToLineItem: migrating ===========================================
-- add_column(:line_items, :order_id, :integer)
-> 0.0017s
== AddOrderIdToLineItem: migrated (0.0019s) ==================================
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)
* activerecord-deprecated_finders (0.0.1 2125c7b)
* activeresource (4.0.0.beta df43ecd)
* activesupport (4.0.0.beta)
* arel (3.0.2.20120819075748 6bcd287)
* builder (3.1.4)
* bundler (1.2.3)
* coffee-rails (4.0.0.beta ce30793)
* coffee-script (2.2.0)
* coffee-script-source (1.4.0)
* erubis (2.7.0)
* execjs (1.4.0)
* hike (1.2.1)
* i18n (0.6.1)
* journey (2.0.0.20120723141804 5e14f74)
* jquery-rails (2.1.4)
* json (1.7.5)
* kaminari (0.14.1)
* mail (2.5.3)
* mime-types (1.19)
* minitest (4.3.3)
* multi_json (1.4.0)
* polyglot (0.3.3)
* rack (1.4.1)
* rack-test (0.6.2)
* rails (4.0.0.beta 8110035)
* railties (4.0.0.beta)
* rake (10.0.2)
* rdoc (3.12)
* sass (3.2.3)
* sass-rails (4.0.0.beta e1bc5aa)
* sprockets (2.8.1)
* sprockets-rails (2.0.0.rc1 1d73bb9)
* sqlite3 (1.3.6)
* thor (0.16.0)
* tilt (1.3.3)
* treetop (1.4.12)
* turbolinks (0.6.1)
* 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])
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>
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>Email</th>
<th>Pay type</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<%= 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, method: :delete,
data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tbody>
</table>
<br />
<%= link_to 'New Order', new_order_path %>
<p><%= 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 |
| 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
12.4 Playtime
12.2 Iteration G2: Atom Feeds