Skip to content

Commit

Permalink
Implemented nested comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
adammacleod committed Oct 5, 2012
1 parent 6d38677 commit b0d790f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
9 changes: 8 additions & 1 deletion app/assets/stylesheets/application.css
Expand Up @@ -22,6 +22,13 @@
color: white !important;
}
textarea {
width: 100% !important;
width: 500px !important;
height: 10em !important;
}
.nested-comment {
padding-left: 5px;
border-left: 5px solid #BBB;
}
.comment form {
display: none;
}
11 changes: 11 additions & 0 deletions app/controllers/comments_controller.rb
Expand Up @@ -5,6 +5,7 @@ class CommentsController < ApplicationController
# GET /links/:link_id/comments.json
def index
@link = Link.find_by_slug(params[:link_id])
@comments = @link.comments.where(:comment_id => nil)

respond_to do |format|
format.html # index.html.erb
Expand Down Expand Up @@ -70,4 +71,14 @@ def update
def destroy
redirect_to Comment.find_by_id(params[:id])
end

# POST /links/:link_id/comments/:id/reply
def reply
@parent = Comment.find_by_id(params[:id])
@comment = @parent.comments.create(params[:comment])
@comment.user = @current_user
@comment.link = @parent.link
@comment.save
head :created
end
end
3 changes: 2 additions & 1 deletion app/controllers/users_controller.rb
Expand Up @@ -8,14 +8,15 @@ def index

respond_to do |format|
format.html { render action: "show" }
format.json { render json: @current_user }
format.json { render json: @user }
end
end

# GET /users/username
# GET /users/username.json
def show
@user = User.find_by_username(params[:id])
@no_replies = true

respond_to do |format|
format.html # show.html.erb
Expand Down
18 changes: 17 additions & 1 deletion app/views/comments/_comment.html.erb
@@ -1,8 +1,24 @@
<div>
<div class="comment">
<span class="label label-info"><%= link_to comment.user.username, comment.user %></span>
<p>
<%= comment.body %><br>
<%= link_to 'parent', link_comments_path(comment.link) %>
| <%= link_to_function 'reply', reply_link_comment_path(comment.link, comment) %>
| <%= time_ago_in_words(comment.created_at) %>
</p>
<div>
<%= form_for(:comment, :url => reply_link_comment_path(comment.link, comment)) do |f| %>
<div class="field">
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>
<div class="nested-comment">
<% if !@no_replies and comment.comments %>
<%= render comment.comments %>
<% end %>
</div>
</div>
2 changes: 1 addition & 1 deletion app/views/comments/index.html.erb
Expand Up @@ -4,7 +4,7 @@

<h3>Comments</h3>

<%= render :partial => 'comments', :locals => { :comments => @link.comments } %>
<%= render :partial => 'comments', :locals => { :comments => @comments } %>
<% if @current_user %>
<hr>
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Expand Up @@ -4,7 +4,11 @@
resource :logins

resources :links do
resources :comments
resources :comments do
member do
post 'reply'
end
end
member do
post 'upvote'
post 'downvote'
Expand Down

0 comments on commit b0d790f

Please sign in to comment.