From b0d790f04558ec5d385225afaf77e6d392b61f1d Mon Sep 17 00:00:00 2001 From: Adam MacLeod Date: Fri, 5 Oct 2012 12:39:31 +1000 Subject: [PATCH] Implemented nested comments. --- app/assets/stylesheets/application.css | 9 ++++++++- app/controllers/comments_controller.rb | 11 +++++++++++ app/controllers/users_controller.rb | 3 ++- app/views/comments/_comment.html.erb | 18 +++++++++++++++++- app/views/comments/index.html.erb | 2 +- config/routes.rb | 6 +++++- 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index f016434..9e4efac 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -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; +} diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 0d44fd5..0e3cf5a 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index e8fa693..b8fd1e7 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -8,7 +8,7 @@ def index respond_to do |format| format.html { render action: "show" } - format.json { render json: @current_user } + format.json { render json: @user } end end @@ -16,6 +16,7 @@ def index # 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 diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index fb1985b..04831eb 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -1,8 +1,24 @@ -
+
<%= link_to comment.user.username, comment.user %>

<%= comment.body %>
<%= 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) %>

+
+ <%= form_for(:comment, :url => reply_link_comment_path(comment.link, comment)) do |f| %> +
+ <%= f.text_area :body %> +
+
+ <%= f.submit %> +
+ <% end %> +
+
+ <% if !@no_replies and comment.comments %> + <%= render comment.comments %> + <% end %> +
diff --git a/app/views/comments/index.html.erb b/app/views/comments/index.html.erb index 8b97b22..81b1e31 100644 --- a/app/views/comments/index.html.erb +++ b/app/views/comments/index.html.erb @@ -4,7 +4,7 @@

Comments

-<%= render :partial => 'comments', :locals => { :comments => @link.comments } %> +<%= render :partial => 'comments', :locals => { :comments => @comments } %> <% if @current_user %>
diff --git a/config/routes.rb b/config/routes.rb index 65bb888..507462a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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'