如何自定义will_paginate链接库? [英] How to customize the will_paginate link base?
本文介绍了如何自定义will_paginate链接库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<%= 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>
?
What should I do if I want to change the href base on /contents
, etc. <a href="/contents?page=n">a link</a>
?
似乎没有选项,帮助!
推荐答案
您可能需要编写自己的LinkRenderer。请参阅此博文,和LinkRenderer的代码。
You will probably have to write your own LinkRenderer. See this blog post, and the code for LinkRenderer.
简要说明:
在 environment.rb
你需要这样的东西:
in environment.rb
you need something like this:
WillPaginate::ViewHelpers.pagination_options[:renderer] = 'MyLinkRenderer'
in 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
这篇关于如何自定义will_paginate链接库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文