How to change will_paginate urls?

I am trying to change the will_paginate links in the format /list?page=1 to /list/page/1 . I have a route setup for this, but will_paginate uses the query string in the links, not my pretty URL style. How can I say otherwise?

I am using will_paginate 2.3.16 and Rails 2.3.14 .

+6
source share
2 answers

will_paginate uses the url_for . 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 that this route is listed above any routes that may also correspond to your controller. Even better: exclude the action of the index as follows:

 map.resources :lists, :except => [:index] 
+9
source

You have several options:

Option 1: the 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 suppose you can do something like "/list/page/#{page}" instead.

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

+1
source

Source: https://habr.com/ru/post/918026/


All Articles