creddit/app/controllers/comments_controller.rb

66 lines
1.3 KiB
Ruby
Raw Normal View History

2015-07-16 11:57:27 -04:00
class CommentsController < ApplicationController
before_filter :set_comment, only: [:show, :edit, :update, :destroy]
before_filter :set_post
before_filter :set_subcreddit
def show
2015-12-11 15:11:06 -05:00
@comments = @comment
.subtree
.includes(:post, :user)
.arrange(order: :created_at)
2015-07-16 11:57:27 -04:00
end
def new
@comment = Comment.new(params[:parent_id])
end
def create
@comment = @post.comments.build comment_params
@comment.user = current_user
if @comment.save
flash[:notice] = 'Comment saved'
else
flash[:alert] = 'Comment could not be saved'
end
redirect_to subcreddit_post_path(@subcreddit, @post)
end
def edit
end
def update
if @comment.update comment_params
redirect_to subcreddit_post_path(@subcreddit, @post),
notice: 'Comment updated'
else
render :edit
end
end
def destroy
@comment.destroy
redirect_to subcreddit_post_path(@subcreddit, @post),
notice: 'Comment deleted'
end
private
def set_subcreddit
@subcreddit = Subcreddit.friendly.find(params[:subcreddit_id])
end
def set_post
@post = Post.find(params[:post_id])
end
def set_comment
@comment = Comment.find(params[:id])
end
def comment_params
params.require(:comment).permit(:parent_id, :content)
end
end