public
Created

Author controller for simple example

  • Download Gist
authors_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 AuthorsController < ApplicationController
# GET /authors
# GET /authors.xml
def index
@author = Author.includes(:books).find(params[:author_id])
 
respond_to do |format|
format.json { render :json => @authors }
format.html # index.html.erb
format.xml { render :xml => @authors }
end
end
 
# GET /authors/1
# GET /authors/1.xml
def show
@author = Author.find(params[:id])
 
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @author }
end
end
 
# GET /authors/new
# GET /authors/new.xml
def new
@author = Author.new
 
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @author }
end
end
 
# GET /authors/1/edit
def edit
@author = Author.find(params[:id])
end
 
# POST /authors
# POST /authors.xml
def create
@author = Author.new(params[:author])
 
respond_to do |format|
if @author.save
format.html { redirect_to(@author, :notice => 'Author was successfully created.') }
format.json { render :json => @author }
format.xml { render :xml => @author, :status => :created, :location => @author }
else
format.html { render :action => "new" }
format.json { render :json => @author.errors.to_a, :status => :unprocessable_entity }
format.xml { render :xml => @author.errors, :status => :unprocessable_entity }
end
end
end
 
# PUT /authors/1
# PUT /authors/1.xml
def update
@author = Author.find(params[:id])
 
respond_to do |format|
if @author.update_attributes(params[:author])
format.html { redirect_to(@author, :notice => 'Author was successfully updated.') }
format.json { render :json => @author }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.json { render :json => @author.errors.to_a, :status => :unprocessable_entity}
format.xml { render :xml => @author.errors, :status => :unprocessable_entity }
end
end
end
 
# DELETE /authors/1
# DELETE /authors/1.xml
def destroy
@author = Author.find(params[:id])
@author.destroy
 
respond_to do |format|
format.html { redirect_to(authors_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.