Isnor Creative
Isnor Creative Blog
Ruby, Ruby on Rails, Ember, Elm, Phoenix, Elixir, React, Vue

Apr 27, 2008

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.

Gordon B. Isnor

Gordon B. Isnor writes about Ruby on Rails, Ember.js, Elm, Elixir, Phoenix, React, Vue and the web.
If you enjoyed this article, you may be interested in the occasional newsletter.

I am now available for project work. I have availability to build greenfield sites and applications, to maintain and update/upgrade existing applications, team augmentation. I offer website/web application assessment packages that can help with SEO/security/performance/accessibility and best practices. Let’s talk

comments powered by Disqus