7

I'm trying to change the will_paginate links from the format of /list?page=1 to /list/page/1. I have the routes setup right for this, but will_paginate is using the query string in links rather than my pretty URL style. How to tell it otherwise?

I'm using will_paginate 2.3.16 and Rails 2.3.14.

Ben G
  • 26,091
  • 34
  • 103
  • 170

2 Answers2

10

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]
iblue
  • 29,609
  • 19
  • 89
  • 128
  • 1
    Thanks. I just had my routes screwed up! Thanks for your answer and here's a bounty – Ben G Jun 18 '12 at 19:07
  • 4
    Now `map.connect` called `match '/list/page/:page' => 'lists#index'`. Anw, there is a typo on your `map.connnect` @iblue, just saying because this post was useful for me :D Please correct me if I'm wrong, rails newbie – ksugiarto Oct 14 '13 at 03:46
2

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.

iblue
  • 29,609
  • 19
  • 89
  • 128
Roman
  • 13,100
  • 2
  • 47
  • 63