diff --git a/app/controllers/v1/articles_controller.rb b/app/controllers/v1/articles_controller.rb index df7dcfb..f3cd9de 100644 --- a/app/controllers/v1/articles_controller.rb +++ b/app/controllers/v1/articles_controller.rb @@ -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 diff --git a/app/controllers/comments_controller.rb b/app/controllers/v1/comments_controller.rb similarity index 62% rename from app/controllers/comments_controller.rb rename to app/controllers/v1/comments_controller.rb index abc2d3b..918d8f4 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/v1/comments_controller.rb @@ -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