Skip to content

Please provide a working example of overriding LinkRenderer #158

ghost opened this Issue Aug 15, 2011 · 17 comments
ghost commented Aug 15, 2011

I can override LinkRenderer in the Git version, but when I call it I get a NotImplementedError for the #url method. It's not clear what the "correct" way to override this functionality is, but overriding #to_html alone does not appear to be sufficient.

If you could add an article on how to do this, it would really help. Thanks!

jrust commented Sep 8, 2011

FWIW, here's an example in my rails 3 app that does google-like pagination. Based off mislav's comment I inherited my custom renderer from the WP::ActionView::LinkRenderer and all the NotImplemented errors disappeared.

# Only show the next 5 subsequent pages. i.e. <- prev 2,3,4,5,6 next ->
module SearchPagination
  class LinkRenderer < WillPaginate::ActionView::LinkRenderer
    def windowed_page_numbers
      inner_window = @options[:inner_window].to_i
      window_from = current_page
      window_to = current_page + inner_window

      # adjust upper limit if out of bounds
      window_to = total_pages if window_to > total_pages
      left = (window_from..window_to).to_a
      left << :gap if total_pages > window_to
avishai commented Sep 9, 2011

Where would you put such a file?

jrust commented Sep 9, 2011

I just put it in app/helpers and then called the pagination helper with the renderer, i.e.

will_paginate @results, :renderer => SearchPagination::LinkRenderer

I created a link renderer to generate markup to work with the Twitter Bootstrap CSS. The renderer is available in a Gist here:

module BootstrapPaginationHelper
    class LinkRenderer < WillPaginate::ActionView::LinkRenderer

      def page_number(page)
        unless page == current_page
          link(page, page, :rel => rel_value(page))
          link(page, "#", :class => 'active')

      def gap
        text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
        %(<li class="disabled"><a>#{text}</a></li>)

      def next_page
        num = @collection.current_page < @collection.total_pages && @collection.current_page + 1
        previous_or_next_page(num, @options[:next_label], 'next')

      def previous_or_next_page(page, text, classname)
        if page
          link(text, page, :class => classname)
          link(text, "#", :class => classname + ' disabled')

      def html_container(html)
        tag(:div, tag(:ul, html), container_attributes)


        def link(text, target, attributes = {})
        if target.is_a? Fixnum
          attributes[:rel] = rel_value(target)
          target = url(target)

                unless target == "#"
                    attributes[:href] = target

        classname = attributes[:class]
        tag(:li, tag(:a, text, attributes), :class => classname)
jnwelzel commented Nov 3, 2011

@oparrish I had a slightly different version here, but ever since I upgraded to 3.0.2 it just stopped working. Thanks for sharing your code, now my renderer works flawlessly.


/+ bootstrap pagination layout for default

hemju commented Dec 12, 2011

@oparrish thx!

tomukas commented Dec 14, 2011

How to load oparrish module as default renderer for will_paginate?

This doesnt work:
WillPaginate::ViewHelpers.pagination_options[:renderer] = 'BootstrapPaginationHelper::LinkRenderer'

tomukas commented Dec 14, 2011

Thanks oparish, I got it working in the view with :renderer parameter. I just want to use it as default renderer for all will_paginate calls like <%=will_paginate @something %>

robacarp commented Jan 4, 2012


I forked @oparrish 's gist and added a few files showing how I do this. I basically wrote a helper method and stuck it in application_helper.rb that calls will_paginate with the right renderer. That way when the code changes I can just tweak the interface in one place.


@oparrish You are the man!! Thank you


@oparrish and @robacarp you both saved my night. was searching like these for hours. thanks a lot

mislav commented Jan 10, 2013

No example yet, but a start of a wiki page:

@mislav mislav closed this Jan 10, 2013
@herestomwiththeweather herestomwiththeweather referenced this issue in oscurrency/oscurrency Jul 13, 2013

bootstrap: style pagination links #402


I created a gist with a fully working rails AJAX paginator with bootstrap 4 styling applied (should work for bootstrap 3 too). You can find it here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.