|
- class ThreadsController < ApplicationController
- # GET /api/threads
- def index
- threads = Topic.includes(:posts).order(updated_at: :desc)
- render json: threads.map { |t|
- t.as_json(methods: [:post_count])
- }
- end
-
- # GET /api/threads/:id
- def show
- thread = Topic.find(params[:id])
- render json: thread
- end
-
- # POST /api/threads
- def create
- thread = Topic.new(thread_params)
- if thread.save
- render json: thread.as_json, status: :created
- else
- render json: { errors: thread.errors.full_messages }, status: :unprocessable_entity
- end
- end
-
- private
-
- def thread_params
- params.require(:thread).permit(:name, :description)
- end
- end
|