will_paginate question

I have a Persons table created with scaffold. I was trying to flick between records by clicking on Previous and Next links. I was struggling to understand will_paginate, and finally came up with following solution, and I'm wondering if it could be done better. Would it be better to have one query to a database returning previous current and next record instead having two?

@person_previous = Person.paginate :all, :page => params[:page],   :order => 'id DESC', :per_page => 1, :conditions => "id < #{params[:id]}" @person_next = Person.paginate :all, :page => params[:page],   :order => 'id ASC', :per_page => 1, :conditions => "id > #{params[:id]}"

<% @person_previous.each do |el|%> <%= link_to 'Previous', el %> <% end %>

<% @person_next.each do |el|%> <%= link_to 'Next', el %> <% end %>

Hi Bigos,

You aren't using the view helper will_paginate gives you. It's as simple as:

@people = Person.paginate :page => params[:page]

And in your view:

<%= will_paginate @posts %>

This will automatically generate the 'Previous' and 'Next' links. For further questions there is a dedicated will_paginate group at http://groups.google.com/group/will_paginate -- they'll be able to help you better!

-- Evan

Rewriting your example a little, I think I would do it like this:

controller

@person = Person.find(params[:id]) @person_previous = Person.first, :order => ‘id DESC’, :conditions => [“id < ?”, @person.id]

@person_next = Person.first, :order => ‘id ASC’, :conditions => [“id > ?”, @person.id]

view

<%= link_to ‘Previous’, @person_previous %>

<%= link_to ‘Next’, @person_next %>

  • removed the paging
  • secured the conditions against sql injection

– Lasse

Jacek Podkanski wrote:

I have a Persons table created with scaffold. I was trying to flick between records by clicking on Previous and Next links. I was struggling to understand will_paginate, and finally came up with following solution, and I'm wondering if it could be done better. Would it be better to have one query to a database returning previous current and next record instead having two?

@person_previous = Person.paginate :all, :page => params[:page],   :order => 'id DESC', :per_page => 1, :conditions => "id < #{params[:id]}" @person_next = Person.paginate :all, :page => params[:page],   :order => 'id ASC', :per_page => 1, :conditions => "id > #{params[:id]}"

<% @person_previous.each do |el|%> <%= link_to 'Previous', el %> <% end %>

<% @person_next.each do |el|%> <%= link_to 'Next', el %> <% end %>

First, yeah, never directly interpolate query parameters (or any user input) into your SQL queries. Second, use the paginate method to only fetch the records relevant for the current page, as the will_paginate view helper takes care of the rest. Did you look at the documentation (gitrdoc.com) for the will_paginate method? It's designed to generate the Previous, Next, and anything-in-between links for you...

ESSENTIALLY, you just wanna do this in your controller:

@people = Person.paginate, :per_page => 1, :page => params[:page], :order => "id ASC"

and then in your view just

<%= will_paginate @people, :page_links => false %>

The gem does pretty much everything for you, just take the time to understand the documentation and the examples, and read the source for anything that's still doesn't make sense.

As I understand it, what he wants to do is not really pagination. Pagination is users?page=1 but what he wants is to be at user/10 and when he clicks next, it goes to user/11, when previous, user/9.

–Lasse

Oops, of course the url’s should be users/10, users/9 and users/11 :slight_smile:

–Lasse

Lasse Bunk wrote:

As I understand it, what he wants to do is not really pagination. Pagination is users?page=1 but what he wants is to be at user/10 and when he clicks next, it goes to user/11, when previous, user/9.

--Lasse

Thats definitely still pagination. Shall we can leave it as an "exercise for the reader" to use the more "resourceful" type links?

Eureka!!!

Now I know why I was getting urls like users?page2. I have learned a lot over last few days, but please forgive me asking silly questions. I still get confused, perhaps I'm trying to learn to much too soon. Anyway, thank you all for the code examples, I will have a closer look at them tomorrow, after having some sleep.

Thank you very much,

Jack

After some thinking I finally decided to use following code:

in controller: @jobseeker_previous = Jobseeker.find :all, :order => 'id DESC', :conditions => ["id < ?",@jobseeker.id], :limit => 1 @jobseeker_next = Jobseeker.find :all, :order => 'id ASC', :conditions => ["id > ?",@jobseeker.id], :limit => 1

in view: <%@jobseeker_previous.each do |el|%> <%= link_to 'Previous', el %> <% end %>

<%@jobseeker_next.each do |el|%> <%= link_to 'Next', el %> <% end %>

I tried few solutions and this one, where I get a collection containing one element, seems to be most convenient for handling first and last record, and is more idiot proof than unnecessarily having to test for nil object.