0

My current setup is that a discussion has many posts. Thus, the show action of discussions shows a list of posts.

discussions/show.html.erb:

<% for post in @discussion.posts %>
<div class="post" id="<%= post.id %>">
    <div class="post-content">
        <div class="post-user">
            <div class="name"><%= link_to post.user.username, post.user %></div>
        </div>
        <div class="post-body">
            <%= post.content %>
        </div>
    </div>
</div>
<% end %>

And this is my discussions_controller show action:

def show
   @forum = Forum.find_by_permalink(params[:forum_id])
   @discussion = Discussion.find(params[:id])
end

Every time I attempt to add the paginate method to my view, I get a series of errors. I know I'm stepping on the wrong foot here, so where should I start to be able to get paginate working for this page?

Many thanks in advance, still somewhat new to Rails!

Kobius
  • 674
  • 7
  • 28

1 Answers1

1

change

@discussion = Discussion.find(params[:id])

to

@posts = Discussion.find(params[:id]).posts.page(params[:page]).per(20) 

Your view would then be

<% for post in @posts %>

Then you'd add the paginate code in the view

<%= paginate(@posts) %>
RadBrad
  • 7,234
  • 2
  • 24
  • 17
  • That worked, thank you! I did have to remove a few 'paths' in my view, is there still a way I can set `@discussion` in my controller? Certain elements do rely on it such as `@discussion.title` and `edit_forum_discussion_path(@forum, @discussion)`? Many thanks. – Kobius Apr 24 '12 at 21:47
  • Ah ignore that, I was over-thinking it. Thanks again for your help! Works perfectly :) – Kobius Apr 24 '12 at 21:54