ぼざクリ タグ広場 https://hub.nizika.monster
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.
 
 
 
 
 
 

52 lines
1001 B

  1. class PostsController < ApplicationController
  2. before_action :set_post, only: %i[ show update destroy ]
  3. # GET /posts
  4. def index
  5. @posts = Post.all
  6. render json: @posts
  7. end
  8. # GET /posts/1
  9. def show
  10. render json: @post
  11. end
  12. # POST /posts
  13. def create
  14. @post = Post.new(post_params)
  15. if @post.save
  16. render json: @post, status: :created, location: @post
  17. else
  18. render json: @post.errors, status: :unprocessable_entity
  19. end
  20. end
  21. # PATCH/PUT /posts/1
  22. def update
  23. if @post.update(post_params)
  24. render json: @post
  25. else
  26. render json: @post.errors, status: :unprocessable_entity
  27. end
  28. end
  29. # DELETE /posts/1
  30. def destroy
  31. @post.destroy!
  32. end
  33. private
  34. # Use callbacks to share common setup or constraints between actions.
  35. def set_post
  36. @post = Post.find(params.expect(:id))
  37. end
  38. # Only allow a list of trusted parameters through.
  39. def post_params
  40. params.expect(post: [ :title, :body ])
  41. end
  42. end