Rails Paginating Links With Nested Routes
This is my solution for a paginating links helper that works with nested routes. It works in combination with the Paginating Find plugin created by the fine folks at Cardboard Rocket, a plugin that you will obviously have to install and apply to your find for this to work. I have a tutorial for that here that may be of assistance.
First you’ll want a partial
Set this up however you like. It relies on the Rails ‘request path’ method which will give you the current url path minus the ?page variable, then it concatenates the result with the various paging variables provided by Paginating Find, converted to string with the to_s method. `
Current Page: <%= @object.page %> |
Total Pages: <%= @object.page_count %><br/>
<%= link_to 'First', request.path + '?page='+@object.first_page.to_s %> |
<%= link_to 'Last', request.path + '?page='+ @object.last_page.to_s %> |
<%= link_to 'Next', request.path + '?page='+@object.next_page.to_s %> |
<%= link_to 'Previous', request.path + '?page='+@object.previous_page.to_s %>
</p>
`
Then you’ll want an Application Helper
` def paging(object) @object = object render :partial => ‘path-to-your-partial/paging’
end
`
Then it’s a simple matter of invoking the helper from your various views. - here I’m assuming you’re showing a listing of @widgets.
<%= paging(@widgets)%>
I am available for Ruby on Rails consulting work – get in touch to learn more.