Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Api::V1::BooksController < ApplicationController
- before_action :load_book, only: [:show]
- def index
- @books = Book.all
- json_response "Indexed books successfully bro", true, {books: books}, :ok
- end
- def show
- json_response "Showed book successfully bro", true, {book: book}, :ok
- end
- def create
- book = Book.new book_params
- if book.save
- json_response "book created successfully bro", true, {book: book}, :ok
- else
- json_response "book creation failed bro", false, {book: book}, :unprocessable_entity
- end
- end
- private
- def load_book
- @book = Book.find_by id: params[:id]
- unless @book.present?
- json_response "Can't get book bro", false, {}, :not_found
- end
- end
- def book_params
- params.require[:book].permit(:author, :title)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement