Reputation: 3622
I'm using newes Rails 3 version with will_paginate.
@videos = user.youtube_videos.sort.paginate :page => page
I also added the @@per_page
attribute to my youtube_video-model.
But it just won't paginate it. I get always all items in the collection listed.
What have I done wrong?
Yours, Joern.
Upvotes: 0
Views: 900
Reputation: 3622
Here's my solution, my own answer, for all other's having trouble with will_paginate and reading this issue:
Create an ApplicationController method like this:
def paginate_collection(collection, page, per_page)
page_results = WillPaginate::Collection.create(page, per_page, collection.length) do |pager|
pager.replace(collection)
end
collection = collection[(page - 1) * per_page, per_page]
yield collection, page_results
end
Then in your Controller, where you got the collection that should be paginated:
page = setup_page(params[:page]) # see below
@messages = Message.inbox(account)
paginate_collection(@messages, page, Message.per_page) do |collection, page_results|
@messages = collection
@page_results = page_results
end
And in your View:
<% @messages.each do |message| %>
<%# iterate and show message titles or whatever %>
<% end %>
<%= will_paginate @page_results %>
To get the page
variable defined, check this:
def setup_page(page)
if !page.nil?
page.to_i
else
1
end
end
So page = setup_page(params[:page])
does the trick, with that simple method.
This WORKS!
Upvotes: 1
Reputation: 107728
Why are you calling sort
here? That seems unnecessary, and probably would result in it finding all videos and calling pagination on that rather than paying any attention to any variable defined in your Video
model. Instead, move the sorting logic into the Video
model by using a scope or use the order
method.
Upvotes: 2