How to change will_paginate URLs?

一曲冷凌霜 提交于 2019-12-05 05:16:09

will_paginate uses the url_for helper. If you define routes containing the parameters it uses, it should generate pretty urls.

map.connect '/lists/page/:page', :controller => 'lists', :action => 'index'

Please make sure, you define this route above any routes that could also match your controller action. Even better: exclude the index action like this:

map.resources :lists, :except => [:index]
Roman

You have several options here:

1st option: monkey patch WillPaginate::ActionView::LinkRenderer#url, which currently has the following url logic:

def url(page)
  @base_url_params ||= begin
    url_params = merge_get_params(default_url_params)
    merge_optional_params(url_params)
  end

  url_params = @base_url_params.dup
  add_current_page_param(url_params, page)

  @template.url_for(url_params)
end

So, I imagine that you can do something like "/list/page/#{page}" instead.

The other way is to fully implement the renderer (by subclassing WillPaginate::ViewHelpers::LinkRenderer), and then providing it as :renderer => MyRendererClass when calling will_paginate.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!