will_paginate question

32 views
Skip to first unread message

Bigos

unread,
Feb 15, 2010, 2:53:55 PM2/15/10
to Ruby on Rails: Talk
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 %>

Lasse Bunk

unread,
Feb 15, 2010, 3:42:43 PM2/15/10
to rubyonra...@googlegroups.com
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

2010/2/15 Bigos <ruby....@googlemail.com>

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.


Paul Harrington

unread,
Feb 15, 2010, 4:27:09 PM2/15/10
to rubyonra...@googlegroups.com

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
(http://gitrdoc.com/mislav/will_paginate/tree/master/) 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.
--
Posted via http://www.ruby-forum.com/.

EvanC

unread,
Feb 15, 2010, 3:25:28 PM2/15/10
to Ruby on Rails: Talk
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

Lasse Bunk

unread,
Feb 15, 2010, 4:51:12 PM2/15/10
to rubyonra...@googlegroups.com
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

2010/2/15 EvanC <ev...@cancelliere.ca>

Lasse Bunk

unread,
Feb 15, 2010, 4:52:17 PM2/15/10
to rubyonra...@googlegroups.com
Oops, of course the url's should be users/10, users/9 and users/11 :)

--Lasse

2010/2/15 Lasse Bunk <lass...@gmail.com>

Paul Harrington

unread,
Feb 15, 2010, 5:04:01 PM2/15/10
to rubyonra...@googlegroups.com
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
>
> 2010/2/15 EvanC <ev...@cancelliere.ca>

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

Bigos

unread,
Feb 15, 2010, 5:40:58 PM2/15/10
to Ruby on Rails: Talk
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

On 15 Feb, 22:04, Paul Harrington <li...@ruby-forum.com> wrote:
> 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
>

> > 2010/2/15 EvanC <e...@cancelliere.ca>

Bigos

unread,
Feb 16, 2010, 5:23:10 AM2/16/10
to Ruby on Rails: Talk
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.

Reply all
Reply to author
Forward
0 new messages