66 lines
1.9 KiB
Ruby
66 lines
1.9 KiB
Ruby
class CommentsController < ApplicationController
|
|
|
|
def edit
|
|
@comment = Comment.find(params[:id])
|
|
if mod? || @comment.author.is?(current_user)
|
|
else
|
|
flash[:alert] = "You are not allowed to edit this comment"
|
|
redirect_to @comment.blogpost
|
|
end
|
|
end
|
|
|
|
def create
|
|
if confirmed?
|
|
@comment = Comment.new(comment_params)
|
|
@comment.user_author = current_user
|
|
@comment.blogpost = Blogpost.find(params[:blogpost_id])
|
|
if @comment.save
|
|
redirect_to blogpost_path(@comment.blogpost) + "#comment-#{@comment.id}", notice: 'Comment created!'
|
|
else
|
|
flash[:alert] = "Could not create comment."
|
|
redirect_to Blogpost.find(params[:blogpost_id])
|
|
end
|
|
else
|
|
flash[:alert] = "You are not allowed to create comments."
|
|
redirect_to Blogpost.find(params[:blogpost_id])
|
|
end
|
|
end
|
|
|
|
def update
|
|
@comment = Comment.find(params[:id])
|
|
if mod? || @comment.author.is?(current_user)
|
|
@comment.user_editor = current_user
|
|
@comment.attributes = comment_params
|
|
if @comment.save
|
|
flash[:notice] = "Comment updated!"
|
|
redirect_to blogpost_path(@comment.blogpost) + "#comment-#{@comment.id}"
|
|
else
|
|
flash[:alert] = "There was a problem while updating your comment"
|
|
render action: "edit"
|
|
end
|
|
else
|
|
flash[:alert] = "You are not allowed to edit this comment"
|
|
redirect_to @comment.blogpost
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@comment = Comment.find(params[:id])
|
|
if mod? || @comment.author.is?(current_user)
|
|
if @comment.destroy
|
|
flash[:notice] = "Comment deleted!"
|
|
else
|
|
flash[:alert] = "There was a problem while deleting this comment"
|
|
end
|
|
else
|
|
flash[:alert] = "You are not allowed to delete this comment"
|
|
end
|
|
redirect_to @comment.blogpost
|
|
end
|
|
|
|
private
|
|
|
|
def comment_params
|
|
params.require(:comment).permit(:content)
|
|
end
|
|
end |