From 348b976254a538ea17bf19a5d53883691be55aed Mon Sep 17 00:00:00 2001 From: Wenbert Del Rosario Date: Sat, 24 Dec 2011 12:11:05 +0800 Subject: [PATCH] added pagination --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/controllers/orders_controller.rb | 4 +++- app/views/orders/index.html.erb | 1 + app/views/store/index.html.erb | 1 + script/load_orders.rb | 6 ++++++ 6 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 script/load_orders.rb diff --git a/Gemfile b/Gemfile index 978fcc0..e599d4b 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,8 @@ gem 'rails', '3.1.3' gem 'sqlite3' +#gem "will_paginate", :git => "https://github.com/p7r/will_paginate.git", :branch => "rails3" +gem 'will_paginate', '~> 3.0' # Gems used only for assets and not required # in production environments by default. diff --git a/Gemfile.lock b/Gemfile.lock index 8b81ee5..2dda2b2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -107,6 +107,7 @@ GEM uglifier (1.2.0) execjs (>= 0.3.0) multi_json (>= 1.0.2) + will_paginate (3.0.2) PLATFORMS ruby @@ -121,3 +122,4 @@ DEPENDENCIES therubyracer turn (= 0.8.2) uglifier (>= 1.0.3) + will_paginate (~> 3.0) diff --git a/app/controllers/orders_controller.rb b/app/controllers/orders_controller.rb index 27d3b66..6fe8f49 100644 --- a/app/controllers/orders_controller.rb +++ b/app/controllers/orders_controller.rb @@ -2,7 +2,9 @@ class OrdersController < ApplicationController # GET /orders # GET /orders.json def index - @orders = Order.all + #@orders = Order.all + @orders = Order.paginate :page=>params[:page], :order=>'created_at desc', + :per_page => 10 respond_to do |format| format.html # index.html.erb diff --git a/app/views/orders/index.html.erb b/app/views/orders/index.html.erb index c96ad3e..de155c4 100644 --- a/app/views/orders/index.html.erb +++ b/app/views/orders/index.html.erb @@ -27,3 +27,4 @@
<%= link_to 'New Order', new_order_path %> +

<%= will_paginate @orders %>

diff --git a/app/views/store/index.html.erb b/app/views/store/index.html.erb index 12e97c8..7a9dec5 100644 --- a/app/views/store/index.html.erb +++ b/app/views/store/index.html.erb @@ -15,3 +15,4 @@
product.id => <%= product.id %>
<% end %> + diff --git a/script/load_orders.rb b/script/load_orders.rb new file mode 100644 index 0000000..984fbde --- /dev/null +++ b/script/load_orders.rb @@ -0,0 +1,6 @@ +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