File size: 815 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Api::CommentsController < ApplicationController
  def create
    @comment = Comment.new(comment_params)
    if @comment.save
      @post = @comment.post
      render "api/posts/show"
    else
      render json: @comment.errors.full_messages
    end
  end

  def index
    @post = Post.find(params[:id])
    @comments = @post.comments
  end

  def update
    @comment = Comment.find(params[:id])
    if @comment.update_attributes(comment_params)
      @post = @comment.post
      render "api/posts/show"
    else
      render json: @comment.errors.full_messages
    end
  end

  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy
    @post = @comment.post
    render "api/posts/show"
  end

  def comment_params
    params.require(:comment).permit(:author_id, :post_id, :body)
  end
end