2

I would be able to paginate easily but I'm using two models which complicates it a bit.

First here's the controller

class BrowseController < ApplicationController

def index
    @hashtags = Hashtag.find(:all, :order => 'created_at DESC') 
    @posts = post.all
end
end

Then here's the view (browse\index.html.erb)

<ul> 
<% @posts.each do |post| %>
<% if post.hashtags.present? %>
<li> <%= link_to post.hashtags.map{|h| "##{h.hashtags}"}.join(', '), post.user %> </li> 
<% else %>

 <% end %> 
 <% end %> 
</ul>

I'm trying to paginate that view. It's not just post, but the hashtag of the posts. What would be the correct <%= will_paginate %> code?

I'm trying to paginate the hashtags of the posts, not just the posts themselves.

rts213
  • 33
  • 1
  • 4
  • 1
    You can't paginate an Array, don't call `.all` on the @posts otherwise you're loading all posts into memory which sort-of works against the entire reason for using pagination – Lee Jarvis May 08 '13 at 08:58
  • I should clarify you *can* paginate an Array, but that's not the default behaviour, I use kaminari with `paginate_array` method but will_paginate works differently: http://stackoverflow.com/questions/13187076/paginating-an-array-in-ruby-with-will-paginate – Lee Jarvis May 08 '13 at 08:59

1 Answers1

3

If you are using the awesome will_paginate gem: https://github.com/mislav/will_paginate, simple change your controller code to:

# per_page 30 posts, change :per_page to the number you´d like
@posts = Post.paginate(:page => params[:page], :per_page => 30)

and change your view to:

<%= will_paginate @posts %>
Matthias
  • 4,355
  • 2
  • 25
  • 34