Updated articles and comments endpoints

This commit is contained in:
mcmac 2018-03-01 12:13:34 +00:00
parent 7551a03e6e
commit ab22669b02
2 changed files with 9 additions and 8 deletions

View file

@ -15,7 +15,7 @@ class V1::ArticlesController < ApplicationController
"next_page": "#{current_page.to_i < total_pages.to_i ? (current_page.to_i+1) : (current_page)}",
"prev_page": "#{current_page.to_i > 1 ? (current_page.to_i-1) : (current_page)}"
}
json_response({articles: @article.as_json(include: [:user, :comments]), pagination: pagination})
json_response({articles: @article.as_json(include: :user), pagination: pagination})
end
# POST /articles
@ -26,7 +26,7 @@ class V1::ArticlesController < ApplicationController
# GET /articles/:id
def show
json_response(@article.to_json(include: :user))
json_response(@article.to_json(include: [:user, :comments]))
end
# PUT /articles/:id

View file

@ -1,11 +1,12 @@
class CommentsController < ApplicationController
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.all
render json: @comments
# @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
@ -15,7 +16,7 @@ class CommentsController < ApplicationController
# POST /comments
def create
@comment = Comment.new(comment_params)
@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
@ -46,6 +47,6 @@ class CommentsController < ApplicationController
# Only allow a trusted parameter "white list" through.
def comment_params
params.require(:comment).permit(:content, :reference, :reference)
params.require(:comment).permit(:content, :article_id, :user_id)
end
end