Skip to content

Instantly share code, notes, and snippets.

@josevalim
Created July 30, 2009 13:47
Show Gist options
  • Save josevalim/158694 to your computer and use it in GitHub Desktop.
Save josevalim/158694 to your computer and use it in GitHub Desktop.
class UsersController < ApplicationController
respond_to :html, :xml
# GET /users
# GET /users.xml
def index
@users = User.all
respond_with(@users)
end
# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])
respond_with(@user)
end
# GET /users/new
# GET /users/new.xml
def new
@user = User.new
respond_with(@user)
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
end
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to(@user) }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
format.html { redirect_to(@user) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
end
class UsersController < ApplicationController
# GET /users
# GET /users.xml
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end
# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
# GET /users/new
# GET /users/new.xml
def new
@user = User.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
end
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to(@user) }
format.xml { render :xml => @user, :status => :created, :location => @user }
else
format.html { render :action => "new" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
format.html { redirect_to(@user) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
end
# This does not work in Rails master. This is a proposal to make respond_with
# completely resourceful so the developer just have to worry with non behavior
# (aka html).
#
# respond_with is checks the action VERB (GET, POST, PUT or DELETE) and acts
# accordingly.
#
# That said, if you are building an API, that is enough for a create action:
#
# def create
# @user = User.new(params[:user])
# @user.save
# respond_with(@user)
# end
#
# If user has no errors (@user.errors.empty? is true), it will respond with
# status created (201) and set the location to the user_url(@user).
#
# If user have errors, it will render the @user.errors with unprocessable
# entity status (422).
#
# Below you have some examples if you need to add html behavior.
#
class UsersController < ApplicationController
respond_to :html, :xml, :json
# GET /users
# GET /users.xml
def index
@users = User.all
respond_with(@users)
end
# GET /users/1
# GET /users/1.xml
def show
@user = User.find(params[:id])
respond_with(@user)
end
# GET /users/new
# GET /users/new.xml
def new
@user = User.new
respond_with(@user)
end
# GET /users/1/edit
def edit
@user = User.find(params[:id])
end
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])
respond_with(@user) do |format|
if @user.save
flash[:notice] = 'User was successfully created.'
format.html { redirect_to(@user) }
else
format.html { render :action => "new" }
end
end
end
# PUT /users/1
# PUT /users/1.xml
def update
@user = User.find(params[:id])
respond_with(@user) do |format|
if @user.update_attributes(params[:user])
flash[:notice] = 'User was successfully updated.'
format.html { redirect_to(@user) }
else
format.html { render :action => "edit" }
end
end
end
# DELETE /users/1
# DELETE /users/1.xml
def destroy
@user = User.find(params[:id])
@user.destroy
respond_with(@user) do |format|
format.html { redirect_to(users_url) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment