Skip to content

Instantly share code, notes, and snippets.

@jkcorrea
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkcorrea/050fd220999afb72210d to your computer and use it in GitHub Desktop.
Save jkcorrea/050fd220999afb72210d to your computer and use it in GitHub Desktop.
Extend will_paginate to work with Materialize
# config/initializers/will_paginate.rb
module WillPaginate
module ActionView
def will_paginate(collection = nil, options = {})
options[:renderer] ||= MaterializeLinkRenderer
super.try :html_safe
end
class MaterializeLinkRenderer < LinkRenderer
protected
def html_container(html)
tag :ul, html, container_attributes
end
def page_number(page)
tag :li,
link(page, page, rel: rel_value(page)),
class: (page == current_page ? 'active' : 'waves-effect')
end
def previous_or_next_page(page, text, classname)
c = classname[0..3]
tag :li,
link(tag(:i, nil, class: "mdi-navigation-chevron-#{c == 'next' ? 'right' : 'left'}"), page || '#!'),
class: [c, classname, ('disabled' unless page)].join(' ')
end
def gap
text = @template.will_paginate_translate(:page_gap) { '&hellip;' }
%(<li><span class="gap">#{text}</span></li>)
end
end
end
end
@toby-1-kenobi
Copy link

I add the 'will_paginate' gem and put this file into the initializers directory.
But, when using <%= will_paginate %> I get this error:
undefined method `total_pages' for nil:NilClass
on line 7
Am I using it wrong?

I'm new to rails and working through railstutorial.org - except using Materialise instead of Bootstrap. I'm up to chapter 9.

@toby-1-kenobi
Copy link

I fixed it by replacing the content of the will_paginate method with this:

  options, collection = collection, nil if collection.is_a? Hash
  collection ||= infer_collection_from_controller
  options = options.symbolize_keys
  options[:renderer] ||= MaterializeLinkRenderer
  super(collection, options)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment