如何自定义will_paginate链接库?

时间:2010-08-07 15:35:28

标签: ruby hyperlink href will-paginate customization

<%= will_paginate(@posts) %> 
# will generate the links like this '<a href="/posts?page=n">a link</a>'

如果我想更改/contents上的href基数等,我该怎么办?<a href="/contents?page=n">a link</a>

似乎没有选择,帮助!

1 个答案:

答案 0 :(得分:6)

您可能需要编写自己的LinkRenderer。请参阅this博文,以及LinkRenderer的code

简言之:

environment.rb中的

你需要这样的东西:

WillPaginate::ViewHelpers.pagination_options[:renderer] = 'MyLinkRenderer'
application_helper.rb

中的

class MyLinkRenderer < WillPaginate::LinkRenderer
  def page_link(page, text, attributes = {})
    url = url_for(page) # you should find a better way to do this...
    url.sub!('posts','contents')
    @template.link_to text, url, attributes
  end
end