Reputation: 203
I am paginating an array @items
Here's the relevant controller part
class StaticPagesController < ApplicationController
def home
if signed_in?
@post = current_user.microposts.build
@activities = PublicActivity::Activity.order("created_at desc").paginate(page: params[:page])
@feed_items = current_user.feed.paginate(page: params[:page])
@items = (@feed_items + @activities)
@items.sort_by! {|item| -item.created_at.to_i}
@items = @items.paginate(:page => 1, :per_page => 10)
else
redirect_to '/signin'
end
end
Basically, this is the line that I'm using.
@items = @items.paginate(:page => 1, :per_page => 10)
I also tried changing that to the code below but didn't work.
@items = @items.paginate(page: params[:page], :per_page => 10)
Inside initializers\will_paginate_array_fix.rb
I have this line
require 'will_paginate/array'
In my view, I am using this
<%= render partial: 'shared/item', collection: @items %>
<%= will_paginate @items %>
It seems to work fine for the first page, but when I click on page 2 or 3 or others, I get a blank. It's null. Anyone know how I can fix this?
Upvotes: 1
Views: 1299
Reputation: 84114
I think your problem is that you are building your array by combining activities and feed items, which are already paginated and then your are paginating it again.
For example assume that your activity and feed item models are using the will paginating default which is something like 20 items per page and that there are 15 activities and 12 feed items.
On page 1 you combine this array,take the top 10 and display them. @items had 27 entries so you show links to page 2 & 3. But when you click on that you're trying to load page 2 of activities and page 2 of feed items, which are empty since there are fewer than 20 of each.
In my experience trying to paginating through several collections like this is very tricky (unless you can load all of them and just paginate the array). A "load more items" button is easier because you just have to keep track of what the last cutoff date was
Upvotes: 3