paginate /comments

This commit is contained in:
joshua stein 2012-09-19 13:40:02 -05:00
parent 2c6eeeca1b
commit 64c6949edf
3 changed files with 28 additions and 2 deletions

View file

@ -1,4 +1,6 @@
class CommentsController < ApplicationController
COMMENTS_PER_PAGE = 20
before_filter :require_logged_in_user_or_400,
:only => [ :create, :preview, :upvote, :downvote, :unvote ]
@ -188,8 +190,18 @@ class CommentsController < ApplicationController
@heading = @title = "Newest Comments"
@cur_url = "/comments"
@comments = Comment.find(:all, :conditions => "is_deleted = 0",
:order => "created_at DESC", :limit => 20, :include => [ :user, :story ])
@page = 1
if params[:page].to_i > 0
@page = params[:page].to_i
end
@comments = Comment.find(
:all,
:conditions => "is_deleted = 0 AND is_moderated = 0",
:order => "created_at DESC",
:offset => ((@page - 1) * COMMENTS_PER_PAGE),
:limit => COMMENTS_PER_PAGE,
:include => [ :user, :story ])
if @user
@votes = Vote.comment_votes_by_user_for_comment_ids_hash(@user.id,

View file

@ -5,3 +5,16 @@
:show_story => true } %>
</ol>
<% end %>
<div class="morelink">
<% if @page && @page > 1 %>
<a href="/comments<%= @page == 2 ? "" : "/page/#{@page - 1}" %>">&lt;&lt;
Page <%= @page - 1 %></a>
<% end %>
<% if @page && @page > 1 %>
|
<% end %>
<a href="/comments/page/<%= @page + 1 %>">Page <%= @page + 1 %> &gt;&gt;</a>
</div>

View file

@ -51,6 +51,7 @@ Lobsters::Application.routes.draw do
post "delete"
post "undelete"
end
get "/comments/page/:page" => "comments#index"
post "/comments/post_to/:story_id" => "comments#create"
post "/comments/preview_to/:story_id" => "comments#preview_new"