Skip to content

Instantly share code, notes, and snippets.

@base10
Created March 25, 2009 00:49
Show Gist options
  • Save base10/84468 to your computer and use it in GitHub Desktop.
Save base10/84468 to your computer and use it in GitHub Desktop.
def create
valid_save = true
Contact.transaction do
## See if we're dealing with a new @contact or an existing contact
## FIXME: Error out if email isn't filled in.
@contact = Contact.find_by_email( params[:contact][:email] )
if @contact.nil?
logger.debug("New contact")
@contact = Contact.new( params[:contact] )
else
logger.debug("Updating contact")
@contact.update_attributes( params[:contact] )
end
if @contact.valid?
@contact.save
## FIXME: I imagine there's a better way to pull the most
## recent response.
@response = @contact.responses[-1]
## TODO: This needs to move to a call for workling/starling
ResponseMailer.deliver_alert @response
else
valid_save = false
## FIXME
logger.error @contact.errors.full_messages
raise ActiveRecord::Rollback
end
end
if valid_save
respond_to do |format|
flash[:message] = "Success!"
format.html { render :action => "show", :status => :created }
format.xml { render :xml => @contact.to_xml, :status => :created }
format.json { render :json => @contact.to_json, :status => :created }
format.yaml { render :text => @contact.to_yaml, :status => :created }
end
else
respond_to do |format|
flash[:message] = "There were errors!"
format.html { render :action => "new", :status => :bad_request }
format.xml { render :xml => @contact.to_xml, :status => :bad_request }
format.json { render :json => @contact.to_json(:include => :responses), :status => :bad_request }
format.yaml { render :text => @contact.to_yaml, :status => :bad_request }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment