Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. class Api::PostsController < ApplicationController
  2. def index
  3. render json: Post.all
  4. end
  5.  
  6. def create
  7. @post = Post.new(post_params)
  8. if @post.save
  9. render json: @post
  10. else
  11. render json: { errors: @post.errors }, status: :unprocessable_entity
  12. end
  13. end
  14.  
  15. def update
  16. @post = Post.find(params[:id])
  17. if @post.update(post_params)
  18. render json: @post
  19. else
  20. render json: { errors: @post.errors }, status: :unprocessable_entity
  21. end
  22. end
  23.  
  24. def destroy
  25. Post.find(params[:id]).destroy
  26. render json: { message: 'post deleted'}
  27. end
  28.  
  29. private
  30. def post_params
  31. # { post: {title: '', body: ''} }
  32. params.require(:post).permit(:title, :body)
  33. end
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement