creddit/app/controllers/comments_controller.rb

79 lines
1.5 KiB
Ruby
Raw Normal View History

2015-12-11 15:26:51 -05:00
# controllers/comments_controller.rb
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
2015-12-14 13:28:39 -05:00
after_action :verify_authorized
2015-07-16 11:57:27 -04:00
def show
2015-12-11 15:11:06 -05:00
@comments = @comment
.subtree
.includes(:post, :user)
.arrange(order: :created_at)
2015-12-14 13:28:39 -05:00
authorize @comment
2015-07-16 11:57:27 -04:00
end
def new
@comment = Comment.new(params[:parent_id])
2015-12-14 13:28:39 -05:00
authorize @comment
2015-07-16 11:57:27 -04:00
end
def create
@comment = @post.comments.build comment_params
@comment.user = current_user
2015-12-14 13:28:39 -05:00
authorize @comment
2015-07-16 11:57:27 -04:00
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
2015-12-14 13:28:39 -05:00
authorize @comment
2015-07-16 11:57:27 -04:00
end
def update
2015-12-14 13:28:39 -05:00
authorize @comment
2015-07-16 11:57:27 -04:00
if @comment.update comment_params
redirect_to subcreddit_post_path(@subcreddit, @post),
notice: 'Comment updated'
else
render :edit
end
end
def destroy
2015-12-14 13:28:39 -05:00
authorize @comment
2015-07-16 11:57:27 -04:00
@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