Skip to content

Instantly share code, notes, and snippets.

@mt9304
Last active October 8, 2018 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mt9304/f8deb96abb3acc0f64d30c47716143e5 to your computer and use it in GitHub Desktop.
Save mt9304/f8deb96abb3acc0f64d30c47716143e5 to your computer and use it in GitHub Desktop.
module Api
class BooksController < ApplicationController
load_and_authorize_resource
before_action :authenticate_user!, except: [:index]
def index
render json: Book.all
end
def new
@book = Book.new
end
def create
book = Book.new(book_params)
if book.save
render json: book
else
render nothing: true, status: :bad_request
end
end
def update
if @book.update(book_params)
render json: @book
else
render nothing: true, status: :unprocessable_entity
end
end
def show
@book = Book.find(params[:id])
render json: @book
end
def destroy
@book.destroy
head :no_content
end
private
def article_params
params.permit(:book, :title, :author, :description, :pages, :published)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment