Skip to content

Instantly share code, notes, and snippets.

@woods
Created September 17, 2009 13:46
Show Gist options
  • Save woods/188497 to your computer and use it in GitHub Desktop.
Save woods/188497 to your computer and use it in GitHub Desktop.
# Create or update a particular user.
#
# PUT /users/:permalink.html
# PUT /users/:permalink.json
# PUT /users/:permalink.xml
#
# If the user does not already exist, it will be created at the given URI.
# If the user already exists, it will be updated. If the permalink is
# updated, the URI of the resource will have changed, and a Location: header
# will be returned with the location of the new resource.
def update
@user = User.find_or_initialize_by_permalink(params[:permalink])
@user.attributes = params[:user]
new_record = @user.new_record?
permalink_changed = @user.changed.include?('permalink') && !new_record
success = @user.save
respond_to do |format|
format.html do
if success
saved = new_record ? 'created' : 'updated'
flash[:success] = "User was successfully #{saved}."
redirect_to @user
else
render :action => :new
end
end
format.json do
if success
if new_record
render :json => @user, :status => :created
else
render :json => @user, :status => :ok
if permalink_changed
headers['Location'] = user_path(@user)
end
end
else
render :json => @user.errors, :status => :unprocessable_entity
end
end
format.xml do
if success
if new_record
render :xml => @user, :status => :created
else
render :xml => @user, :status => :ok
if permalink_changed
headers['Location'] = user_path(@user)
end
end
else
render :xml => @user.errors, :status => :unprocessable_entity
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment