Skip to content

Instantly share code, notes, and snippets.

@phillipkregg
Created January 8, 2012 23:39
Show Gist options
  • Save phillipkregg/1580134 to your computer and use it in GitHub Desktop.
Save phillipkregg/1580134 to your computer and use it in GitHub Desktop.
Author controller for simple example
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment