Created
September 13, 2012 17:48
-
-
Save anonymous/3716160 to your computer and use it in GitHub Desktop.
Help with nesting comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for ([@task, @task.comments.build]) do |f| %> | |
<%= f.text_area :comment %> | |
<%= f.hidden_field :parent_id%> | |
<%= f.submit %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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.... --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment