6

让我们使用一个真实世界的例子。

我想修补 WillPaginate::LinkRenderer.to_html 方法。

到目前为止,我已经尝试过:

  1. 在文件夹中创建了一个文件:lib/monkeys/will_paginate_nohtml.rb
  2. 在 config/environments.rb 中添加:文件末尾需要 'monkeys/will_paginate_nohtml'
  3. 在该文件中,这是我的代码:

e

module Monkeys::WillPaginateNohtml
  def to_html
    debugger
    super
  end
end

WillPaginate::LinkRenderer.send(:include, Monkeys::WillPaginateNohtml)

但不知何故,调试器没有通过。看来补丁失败了。

任何帮助将不胜感激,谢谢!

4

4 回答 4

10

那么这个呢 :-) @shingana,@kandadaboggu 的解决方案将不起作用,因为这里没有“超级”。您想调用原始版本而不是超级版本。

module WillPaginate
  class LinkRenderer
    alias_method :to_html_original, :to_html
    def to_html
      debugger
      to_html_original
    end
  end
end
于 2010-09-30T18:37:11.510 回答
5

The title of your question is misleading. Frankly, I think you probably just want to customize the will_paginate page list structure, which can be done differently.

So in your case the right way is to extend the renderer. For example load the following from an initializer (via config/initializers):

class CustomPaginationRenderer < WillPaginate::LinkRenderer

  def to_html
    # Your custom code, debugger etc
  end

end

Then, to have your application use this renderer add the following to your config/environment.rb file:

WillPaginate::ViewHelpers.pagination_options[:renderer] = 'CustomPaginationRenderer'
于 2010-09-30T19:01:48.493 回答
0

我认为你需要打开方法

module WillPaginate
  class LinkRenderer
    def to_html
      debugger
      super
    end
  end
end
于 2010-09-30T18:01:56.227 回答
0

试试这个:

class WillPaginate::LinkRenderer
  def to_html
    debugger
    super
  end
end
于 2010-09-30T18:16:49.833 回答