class UsersController < ApplicationController
before_filter :authorize
before_filter :find_user, :only => [:edit, :update, :destroy]
# 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/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
end
# POST /users
# POST /users.xml
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to(users_path, :notice => 'User was successfully created.') }
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
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(users_path, :notice => 'User was successfully updated.') }
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.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
protected
def find_user
@user = User.find(params[:id])
end
end
Refactorings
No refactoring yet !
Swami Atma
November 28, 2010, November 28, 2010 12:09, permalink
Use a gem such as inherited_resources (https://github.com/josevalim/inherited_resources). Having installed the gem, replace the above code with the following:
class UsersController < InheritedResources::Base end
Swami Atma
November 28, 2010, November 28, 2010 12:11, permalink
Use a gem such as inherited_resources (https://github.com/josevalim/inherited_resources). Having installed the gem, replace the above code with the following:
class UsersController < InheritedResources::Base end
Any idea how I can refactor this? Reek is telling me there is a lot of duplication with the format.html, .xml stuff. Also says I pass params multiple times in each action.
Thank you in advance!
Andrew Davis