Ruby on Rails 3でwill_paginateを使ってみた。

インストール

GemFileに以下を追加

gem 'will_paginate', '>=3.0.pre2'

bundleでインストール

$ <span style="font-weight:bold;">sudo bundle install</span>
[sudo] password for xxxx: 
Fetching source index for http://rubygems.org/
Using rake (0.8.7) 
Using RedCloth (4.2.3) 
Using abstract (1.0.0) 
Using activesupport (3.0.3) 
Using builder (2.1.2) 
Using i18n (0.5.0) 
Using activemodel (3.0.3) 
Using erubis (2.6.6) 
Using rack (1.2.1) 
Using rack-mount (0.6.13) 
Using rack-test (0.5.6) 
Using tzinfo (0.3.23) 
Using actionpack (3.0.3) 
Using mime-types (1.16) 
Using polyglot (0.3.1) 
Using treetop (1.4.8) 
Using mail (2.2.12) 
Using actionmailer (3.0.3) 
Using arel (2.0.6) 
Using activerecord (3.0.3) 
Using activeresource (3.0.3) 
Using bundler (1.0.0.rc.3) 
Using mysql2 (0.2.6) 
Using thor (0.14.6) 
Using railties (3.0.3) 
Using rails (3.0.3) 
Installing will_paginate (3.0.pre2) 
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

さらにサーバが起動中の場合は再起動する。
サーバの停止はctrl+C
サーバの起動は

$ rails server

利用

コントローラでのモデルの検索はこんな感じ。@booksとかBookは例。

@books = Book.paginate(:page => params[:page], :order => 'created_at desc', :per_page => 4)

Viewでのナビゲーションの表示はこんな感じで出来る。

<%= will_paginate @youtubes %>

こんな感じのができるので、あとはCSSなどで見た目の修正とかすると良い。

← Previous 1 2 3 4 5 6 7 8 9 10 11 12 Next →

(参考)will_paginateのgithubは以下
https://github.com/mislav/will_paginate/wiki