2011年6月28日火曜日

[Rails]: Generate Ajax remote link for Rails 3 + will_paginate 3.0.pre2

The will_paginate 3.0.pre2 Gem does not support remote tag (like :remote => true).
You need to create custom renderer but currently available posts do not work well.


I have solved this problem.

lib/remote_pagination_list_link_renderer.rb
class RemotePaginationListLinkRenderer < WillPaginate::ViewHelpers::LinkRenderer
  private
 
  # :remote => true
  def link(text, target, attributes = {})
    if target.is_a? Fixnum
      attributes[:rel] = rel_value(target)
      target = url(target)
    end
    attributes[:href] = target
    attributes["data-remote"] = true
    attributes["data-method"] = :get
    tag(:a, text, attributes)
  end
end

Then, call will_paginate helper like below.
<%= will_paginate @posts, 
  :renderer => RemotePaginationListLinkRenderer %>
If you want to set this renderer as default, config/initializers/will_paginate.rb
# for :remote option
WillPaginate::ViewHelpers.pagination_options[:renderer] =
  'RemotePaginationListLinkRenderer'

0 件のコメント:

コメントを投稿