Ajax対応したwill_paginate(Version 2.3.5) For Rails2.2.2

will_paginateをAjax対応させる方法をネットで探していて見つけたxibbarさんの記事
http://d.hatena.ne.jp/xibbar/20080612#1213252219

残念ながら私が使っているRailsやwill_paginateのバージョンと噛み合わず動作しなかったため、xibbarさんの記事を参考にAjax対応したレンダラークラスを書いたのでメモ。

class RemoteLinkRenderer < WillPaginate::LinkRenderer
  def page_link(page, text, attributes = {})
    @template.link_to_remote text, remote_url_for(page), attributes
  end
  
  protected
  def remote_url_for(page)
    { :url => url_for(page),
      :update => @options[:update],
      :method => @options[:method] }
  end
end

xibbarさんの記事にも記載されているとおり、上記クラスをrails_root/app/helpersディレクトリ以下にremote_link_renderer.rbという名前(クラス名と対応した名前でなければならない)で保存し、ビューで以下のように呼び出す。

<%= will_paginate @collections, :renderer => RemoteLinkRenderer,
                              :update => 'container',
                              :params=> { :action => 'search' },
                              :method => :get %>

xibbarさん感謝です。

補足)
元々will_paginate(Version 2.3.3) For Rails2.1.0で書かれた記事ですが、タイトルのバージョンでも動作を確認できたのでタイトルを変更しました。