This repository has been archived on 2024-08-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
redstoner.com/app/controllers/comments_controller.rb
2014-04-14 06:26:37 +02:00

64 lines
1.8 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)
if @comment.update_attributes(comment_params)
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