Skip to content

Instantly share code, notes, and snippets.

@melbourne2991
Created January 16, 2014 06:33
Show Gist options
  • Save melbourne2991/8450678 to your computer and use it in GitHub Desktop.
Save melbourne2991/8450678 to your computer and use it in GitHub Desktop.
Nested comments I built on my first rails project before deciding to use disqus instead.
.comment{:class => "c" + nesting.to_s}
.profile
%img{:src => "/assets/profile_image_sample.jpg"}
.message
.username
- if defined? comment.user.username
- if comment.user.username.blank?
= comment.user.first_name
- else
= comment.user.username
- else
= comment.user.first_name
= comment.content
.reply-link
= link_to "Reply to comment...", post_path(:original_id => comment.id)
- if @original_id.to_s == comment.id.to_s
.comment{:class => "c" + nesting.to_s + " reply"}
= render "submit_comment", :nesting => nesting
- if comment.replies.count > 0
- nesting = nesting + 1
- comment.replies.each do |comment|
= render "comment", :comment => comment, :nesting => nesting
class CommentsController < ApplicationController
def create
@user = User.new(user_params)
@post = Post.find(params[:post_id])
respond_to do |format|
if @user.save
@comment = Comment.new(comment_params)
@comment.user_id = @user.id
@comment.post_id = params[:post_id]
if @comment.save
format.html { redirect_to @post, notice: 'Comment was successfully created.' }
else
format.html { redirect_to @post, notice: 'Comment was successfully created.' }
end
else
format.html { redirect_to @post, notice: 'Comment was successfully created.' }
end
end
end
private
def user_params
params.require(:user).permit(:first_name, :email)
end
def comment_params
params.require(:comment).permit(:content, :post_id, :original_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment