Skip to content

Instantly share code, notes, and snippets.

@waseem
Created January 6, 2011 12:34
Show Gist options
  • Select an option

  • Save waseem/767834 to your computer and use it in GitHub Desktop.

Select an option

Save waseem/767834 to your computer and use it in GitHub Desktop.
class TemplatesController < ApplicationController
#
# The template which is going to be destroyed is
# last_accessed_template of current user. If
# template is destroyed and updation fails, user
# in the database will refenrence a destroyed template.
#
# How to handle above situation?
#
# oficina7:
# user has_many :templates; user belongs_to :template; template has_many :users; @user.template (may return nil) @user.template || Template.where(:is_default=>true).first (probably won't return nil)
#
# def template_or_default; template || Template.where(:is_default=>true).first; end;
def destroy
begin
template = current_user.templates.find(params[:id])
template.destroy
current_user.update_attributes(:last_accessed_template => Template.default)
respond_to do |format|
format.xml { head :ok }
end
rescue ActiveRecord::RecordNotFound
respond_to do |format|
format.xml { render :xml => { :errors => { :error => { :description => "You can destroy only your templates." }}}.to_xml,
:status => :unauthorized }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment