Class: Api::CommentsController

Inherits:
ApplicationController
  • Object
show all
Includes:
ActionController::Serialization, CommentOwnerControllerConcern, LoggedInControllerConcern, PostOwnerControllerConcern
Defined in:
app/controllers/api/comments_controller.rb

Instance Method Summary collapse

Methods included from CommentOwnerControllerConcern

#ensure_comment, #ensure_comment_owner, #has_comment_access

Methods included from PostOwnerControllerConcern

#ensure_post, #ensure_post_owner, #has_post_access

Instance Method Details

#createObject

Creates new comment belonging to the post

‘POST /api/boards/:board_id/posts/:post_id/`



22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/api/comments_controller.rb', line 22

def create
  @comment = Comment.new(params.permit(:content))
  @comment.user = @user
  @comment.post = @post
  if @comment.save
    render json: @comment
  else
    render json: @comment.errors, status: 422
  end
end

#destroyObject

Deletes a comment

‘DELETE /api/boards/:board_id/posts/:post_id/comments/:id`



47
48
49
50
# File 'app/controllers/api/comments_controller.rb', line 47

def destroy
  @comment.destroy
  render json: {}, status: :ok
end

#indexObject

Renders the comments belonging to the post

‘GET /api/boards/:board_id/posts/:post_id/`



15
16
17
# File 'app/controllers/api/comments_controller.rb', line 15

def index
  paginate json: @post.comments.page(params[:page]), per_page: 20
end

#updateObject

Updates a comment

‘PUT /api/boards/:board_id/posts/:post_id/comments/:id`



36
37
38
39
40
41
42
# File 'app/controllers/api/comments_controller.rb', line 36

def update
  if @comment.update(params.permit(:content))
    render json: @comment
  else
    render json: @comment.errors, status: 422
  end
end