vereto-api/app/controllers/v1/comments_controller.rb

53 lines
1.4 KiB
Ruby
Raw Normal View History

class V1::CommentsController < ApplicationController
2018-02-27 14:54:19 +00:00
before_action :set_comment, only: [:show, :update, :destroy]
skip_before_action :authorize_request, only: [:index, :show]
2018-02-27 14:54:19 +00:00
# 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)
2018-02-27 14:54:19 +00:00
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)
2018-02-27 14:54:19 +00:00
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)
2018-02-27 14:54:19 +00:00
end
end