I have made a custom link renderer for will_paginate and have placed the code in lib/my_link_renderer.rb
require 'will_paginate/view_helpers/link_renderer'
require 'will_paginate/view_helpers/action_view'
class MyLinkRenderer < WillPaginate::ActionView::LinkRenderer
include ListsHelper
def to_html
html = pagination.map do |item|
item.is_a?(Fixnum) ?
page_number(item) :
send(item)
end.join(@options[:link_separator])
html << @options[:extra_html] if @options[:extra_html]
@options[:container] ? html_container(html) : html
end
end
Then I use it like so:
<%= will_paginate @stuff,
:previous_label=>'<input class="btn" type="button" value="Previous"/>',
:next_label=>'<input class="btn" type="button" value="Next" />',
:extra_html=>a_helper,
:renderer => 'WillPaginate::ActionView::MyLinkRenderer'
%>
It works the first time but the second time I get a uninitialized constant WillPaginate::ActionView::MyLinkRenderer error. I believe I am loading files from lib into my application correctly in my config/application.rb:
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
I get the same problem in the console.
system :001 > WillPaginate::ActionView::MyLinkRenderer
=> MyLinkRenderer
system :002 > WillPaginate::ActionView::MyLinkRenderer
NameError: uninitialized constant WillPaginate::ActionView::MyLinkRenderer
Suspect this has something to do with how rails autoloads things. Should I not be using autoload? Should I be explictly requiring ‘./lib/my_link_renderer’?
I should note this only happens on my production server.
Your
MyLinkRendererclass isn’t in theWillPaginate::ActionViewmodule, so referring to it asWillPaginate::ActionView::MyLinkRenderershould never work.You should either refer to it as
MyLinkRenderer(without the module name) or define it to be in that module, and move it tolib/will_paginate/action_view/my_link_renderer.rb:The fact that it works first time is a quirk of the way Rails uses
const_missingto implement autoloading. If you’re curious, see this answer: https://stackoverflow.com/a/10633531/5168