public
Created

Books Controller for simple example

  • Download Gist
books_controller.rb
Ruby
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
class BooksController < ApplicationController
# GET /books
# GET /books.xml
def index
@books = Book.all
 
respond_to do |format|
format.json { render :json => @posts }
format.html # index.html.erb
format.xml { render :xml => @books }
end
end
 
# GET /books/1
# GET /books/1.xml
def show
@book = Book.find(params[:id])
 
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @book }
end
end
 
# GET /books/new
# GET /books/new.xml
def new
@book = Book.new
 
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @book }
end
end
 
# GET /books/1/edit
def edit
@book = Book.find(params[:id])
end
 
# POST /books
# POST /books.xml
def create
@book = Book.new(params[:book])
 
respond_to do |format|
if @book.save
format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
format.json { render :json => @book }
format.xml { render :xml => @book, :status => :created, :location => @book }
else
format.html { render :action => "new" }
format.json { render :json => @book.errors.to_a, :status => :unprocessable_entity }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
 
# PUT /books/1
# PUT /books/1.xml
def update
@book = Book.find(params[:id])
 
respond_to do |format|
if @book.update_attributes(params[:book])
format.html { redirect_to(@book, :notice => 'Book was successfully updated.') }
format.json { render :json => @book }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @book.errors.to_a, :status => :unprocessable_entity}
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
 
# DELETE /books/1
# DELETE /books/1.xml
def destroy
@book = Book.find(params[:id])
@book.destroy
 
respond_to do |format|
format.html { redirect_to(books_url) }
format.json { render :json => 'ok'.to_json }
format.xml { head :ok }
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.