class CommentsController < ApplicationController
  before_action :find_commentable
  respond_to :js

  def create
    @comment = @commentable.comments.new do |comment|
      comment.comment = params.require(:comment)[:comment]
      comment.user = current_user
    end
    @comment.save
    @commentable.reload
  end

  def destroy
    @comment = current_user.comments.find(params[:id])
    @comment_id = params[:id]
    @comment.destroy
    @commentable.reload
  end

  private

  def find_commentable
    @commentable_type = params[:commentable_type].classify
    @commentable = @commentable_type.constantize.find(params[:commentable_id])
  end
end
