class V1::CommentsController < ApplicationController before_action :set_comment, only: [:show, :update, :destroy] skip_before_action :authorize_request, only: [:index, :show] # GET /comments def index # @comments = Comment.find_by(article_id: params[:article_id]).as_json(include: :user) @comments = Article.find(params[:article_id]).comments.as_json(include: :user) json_response(@comments) end # GET /comments/1 def show render json: @comment end # POST /comments def create @comment = current_user.comments.create!(content: params[:content], article_id: params[:article_id]) # Comment.new(comment_params) if @comment.save render json: @comment, status: :created, location: @comment else render json: @comment.errors, status: :unprocessable_entity end end # PATCH/PUT /comments/1 def update if @comment.update(comment_params) render json: @comment else render json: @comment.errors, status: :unprocessable_entity end end # DELETE /comments/1 def destroy @comment.destroy end private # Use callbacks to share common setup or constraints between actions. def set_comment @comment = Comment.find(params[:id]) end # Only allow a trusted parameter "white list" through. def comment_params params.require(:comment).permit(:content, :article_id, :user_id) end end