9

How can I use will_paginate with a custom route?

I have the following in my routes:

map.connect 'human-readable/:name', :controller => :tags, :action => 'show'

but will_paginate uses url_for as far as I can tell, but I want to use 'human-readable' instead of url_for, but how?

Edit

When I click the paging link generated by will_paginate, it's using:

"tags/show?name=Elektronikindustri&page=1"

Instead of:

"/human-readable/show?name=Elektronikindustri&page=1"

I want will_paginate to use my custom route instead of the actual controller name

Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
kristian nissen
  • 2,809
  • 5
  • 44
  • 68
  • When I click the paging link generated by will_paginate it's using tags/show?name=Elektronikindustri&page=1 and not '/human-readable/show?name=Elektronikindustri&page=1' - how can I change this? 'human-readable' is inside my routes file like this: map.connect 'human-readable/:name', :controller => :tags, :action => 'show' I want will_paginate to use my custom route instead of the actual controller name – kristian nissen Mar 14 '10 at 10:10

3 Answers3

9

The will_paginate view helper has a :params option for overriding the default link generation.

Change your routes configuration:

map.human_readable_tag '/human-readable/:name', 
     :controller => :tags, :action => 'show'

Invoke the will_paginate view helper as follows:

<%= will_paginate @tag_list, 
     :params => {:controller => human_readable_tag_path(@tag_name) } %>

Make sure you have set the @tag_name variable in your controller.

For more information read the will_paginate view helper documentation.

The :params option passed to the helper is used to invoke url_for. So read the url_for documentation for how we faked a controller name.

Harish Shetty
  • 64,083
  • 21
  • 152
  • 198
6

Another option is to use param in will_paginate but passing in the parameter like so:

<%= will_paginate @products, :params => {:controller => 'human-readable', :action => 'show', :name => 'xyz'} %>

and now your links will look like human-readable/xyz?page=2...

Jhony Fung
  • 2,970
  • 4
  • 29
  • 32
0

You need define this route too


map.connect 'human-readable/:name', :controller => :tags, :action => 'show'
map.connect 'human-readable/:name/page/:page', :controller => :tags, :action => 'show'
shingara
  • 46,608
  • 11
  • 99
  • 105
  • Great thank you, how do I get will_paginate to pick this one up? It's still using 'tags'? When I click a link in the pagination it's still using 'tags' and not 'human-readable' – kristian nissen Mar 14 '10 at 12:46