Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. class Api::V1::BooksController < ApplicationController
  2. before_action :load_book, only: [:show]
  3.  
  4. def index
  5. @books = Book.all
  6. json_response "Indexed books successfully bro", true, {books: books}, :ok
  7. end
  8.  
  9. def show
  10. json_response "Showed book successfully bro", true, {book: book}, :ok
  11. end
  12.  
  13. def create
  14. book = Book.new book_params
  15. if book.save
  16. json_response "book created successfully bro", true, {book: book}, :ok
  17. else
  18. json_response "book creation failed bro", false, {book: book}, :unprocessable_entity
  19. end
  20. end
  21.  
  22.  
  23. private
  24.  
  25. def load_book
  26. @book = Book.find_by id: params[:id]
  27. unless @book.present?
  28. json_response "Can't get book bro", false, {}, :not_found
  29. end
  30. end
  31.  
  32.  
  33. def book_params
  34. params.require[:book].permit(:author, :title)
  35. end
  36.  
  37. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement