-
Notifications
You must be signed in to change notification settings - Fork 1
/
books_controller.rb
51 lines (42 loc) · 996 Bytes
/
books_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class BooksController < ApplicationController
before_action :set_book, only: [:show, :update, :destroy]
# GET /books
def index
@books = Book.all
render json: @books
end
# GET /books/1
def show
render json: @book
end
# POST /books
def create
@book = Book.new(book_params)
if @book.save
render json: @book, status: :created, location: @book
else
render json: @book.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /books/1
def update
if @book.update(book_params)
render json: @book
else
render json: @book.errors, status: :unprocessable_entity
end
end
# DELETE /books/1
def destroy
@book.destroy
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def book_params
params.require(:book).permit(:title)
end
end