Challenge 1: A JSON Index Action — Solution # app/controllers/comments_controller.rb class CommentsController < ApplicationController def index comments = Comment.all render json: comments end def create comment = Comment.new(comment_params) if comment.save render json: comment, status: :created else render json: { errors: comment.errors }, status: :unprocessable_entity end end private def comment_params params.require(:comment).permit(:body, :post_id) end end =begin Notes: - render json: comments on the collection serializes every Comment in the relation the same way render json: comment (singular) would serialize one -- Rails calls .to_json across the whole array automatically. - status: :created is the symbol form of HTTP 201, matching what a successful POST should return per REST convention (the same convention express1-7 covered numerically). - On failure, comment.errors is rendered directly as JSON under an errors key, giving the client the same validation-failure detail rails1-8's strong-parameters chapter surfaced through form re-rendering -- just shaped as JSON instead of HTML. - comment_params reuses the exact strong-parameters pattern from rails1-8 -- require(:comment).permit(...) -- unchanged by the fact this is an API action rather than an HTML form submission. =end