32 lines
828 B
Ruby
32 lines
828 B
Ruby
class CommentsController < ApplicationController
|
|
before_action :require_user
|
|
|
|
def create
|
|
@article = Article.find(params[:article_id])
|
|
@comment = @article.comments.new(comment_params)
|
|
@comment.commenter = current_user.username
|
|
|
|
if @comment.save
|
|
flash[:notice] = "Comment added successfully."
|
|
else
|
|
flash[:alert] = "Failed to add comment."
|
|
end
|
|
|
|
redirect_to article_path(@article)
|
|
end
|
|
|
|
def destroy
|
|
@article = Article.find(params[:article_id])
|
|
@comment = @article.comments.find(params[:id])
|
|
if @article.user_id == current_user.id || @comment.commenter == current_user.username
|
|
@comment.destroy
|
|
end
|
|
redirect_to article_path(@article), status: :see_other
|
|
end
|
|
|
|
private
|
|
def comment_params
|
|
params.require(:comment).permit(:body, :status)
|
|
end
|
|
end
|