public
anonymous / _comment.html.erb
Created

Help with nesting comments

  • Download Gist
_comment.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10
<div class="comment_<%= comment.id %>">
<p><%= comment.comment %></p>
<div class="comment_info">
Written by: <%= comment.user.name %> |
You can: <%= link_to 'Reply To Comment', new_task_comment_path(@task.id, :parent_id => comment) %> or
<%= link_to 'Edit Comment', edit_task_comment_path(comment.task.project.id, comment.id) %> |
<%= link_to 'Delete Comment', task_comment_path(@project.id, comment.id), :method => :delete%>
</div>
</div>
_createcomments.html.erb
HTML+ERB
1 2 3 4 5
<%= form_for ([@task, @task.comments.build]) do |f| %>
<%= f.text_area :comment %>
<%= f.hidden_field :parent_id%>
<%= f.submit %>
<% end %>
coments_model.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Comment < ActiveRecord::Base
attr_accessible :comment, :user_id, :parent_id
 
#why do I need parent_id? there’s no column in the db for it....
#According to the documentation and examples, I don't need to have this but it freaks
#out if I dont have it.....TEH FUCK
has_ancestry
belongs_to :task
belongs_to :user
validates :comment, :presence => true
end
comments.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
class CommentsController < ApplicationController
before_filter :find_comment_id, :only => [:show, :edit, :update, :destroy]
def index
@comment = Comment.new
@comments = Comment.all
end
def new
@task = Task.find(params[:task_id])
@task.comments.new(:parent_id => params[:parent_id])
end
def create
@task = Task.find(params[:task_id])
@task_comment = @task.comments.create(params[:comment].merge(:user_id => current_user.id))
if @task_comment.save
redirect_to project_task_path(@task.project, @task), :flash => {:success => 'Created Comment.'}
else
redirect_to project_task_path(@task.project, @task), :flash => {:error => 'Cannot have empty comments.'}
end
end
def show
end
def edit
end
def update
if @comment.update_attributes(params[:comment])
redirect_to project_task_path(@comment.task.project, @comment.task), :flash => {:success => 'Edited the comment'}
else
redirect_to project_task_path(@comment.task.project, @comment.task), :flash => {:error => 'Could not update empty comment'}
end
end
def destroy
@comment.destroy
redirect_to project_task_path(@comment.task.project, @comment.task), :flash => {:success => 'We deleted your comment.'}
end
private
def find_comment_id
@comment = Comment.find(params[:id])
end
end
comments_helper.rb
Ruby
1 2 3 4 5 6 7
module CommentsHelper
def nested_comments(comments)
comments.map do |comment, sub_comments|
render(comment) + content_tag(:div, nested_comments(sub_comments), :class => 'nested_comments')
end.join.html_safe
end
end
example_what_it_should_look_like.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!-- This is what it should, in theory resemble, in structure -->
 
{ #<TreeNode id: 100018, name: "Stinky", ancestry: nil>
=> { #<TreeNode id: 100019, name: "Crunchy", ancestry: "100018">
=> { #<TreeNode id: 100020, name: "Squeeky", ancestry: "100018/100019">
=> {}
}
}
}
 
<!-- But this is what mine looks like.... -->
 
{#<Comment id: 4, comment: "Example Comment", created_at: "2012-09-13 20:37:35", updated_at: "2012-09-13 20:37:35", task_id: 2, user_id: 1, ancestry: nil>=>{}, #<Comment id: 5, comment: "Example Reply\r\n", created_at: "2012-09-13 20:37:42", updated_at: "2012-09-13 20:37:42", task_id: 2, user_id: 1, ancestry: nil>=>{}}
 
<!-- So there is obviously something wrong here.... -->
page_source.html
HTML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<div class="comment_23">
<p>I am a comment</p>
<div class="comment Info">
Written by: Adam |
You can: <a href="/tasks/7/comments/new?parent_id=23">Reply To Comment</a> or
<a href="/tasks/1/comments/23/edit">Edit Comment</a> |
<a href="/tasks/1/comments/23" data-method="delete" rel="nofollow">Delete Comment</a>
</div>
</div>
<div class="nested_comments"></div><div class="comment_24">
<p>I am another comment</p>
<div class="comment Info">
Written by: Adam |
You can: <a href="/tasks/7/comments/new?parent_id=24">Reply To Comment</a> or
<a href="/tasks/1/comments/24/edit">Edit Comment</a> |
<a href="/tasks/1/comments/24" data-method="delete" rel="nofollow">Delete Comment</a>
</div>
</div>
<div class="nested_comments"></div><div class="comment_25">
<p>I am a reply to the first comment</p>
<div class="comment Info">
Written by: Adam |
You can: <a href="/tasks/7/comments/new?parent_id=25">Reply To Comment</a> or
<a href="/tasks/1/comments/25/edit">Edit Comment</a> |
<a href="/tasks/1/comments/25" data-method="delete" rel="nofollow">Delete Comment</a>
</div>
</div>
<div class="nested_comments"></div>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.