You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

32 lines
690 B

  1. class ThreadsController < ApplicationController
  2. # GET /api/threads
  3. def index
  4. threads = Topic.includes(:posts).order(updated_at: :desc)
  5. render json: threads.map { |t|
  6. t.as_json(methods: [:post_count])
  7. }
  8. end
  9. # GET /api/threads/:id
  10. def show
  11. thread = Topic.find(params[:id])
  12. render json: thread
  13. end
  14. # POST /api/threads
  15. def create
  16. thread = Topic.new(thread_params)
  17. if thread.save
  18. render json: thread.as_json, status: :created
  19. else
  20. render json: { errors: thread.errors.full_messages }, status: :unprocessable_entity
  21. end
  22. end
  23. private
  24. def thread_params
  25. params.require(:thread).permit(:name, :description)
  26. end
  27. end