Skip to content

Instantly share code, notes, and snippets.

@jbutz
Last active December 16, 2015 05:59
Show Gist options
  • Save jbutz/5388316 to your computer and use it in GitHub Desktop.
Save jbutz/5388316 to your computer and use it in GitHub Desktop.
Ruby on Rails Exceptions
class Status < ActiveRecord::Base
...
before_destroy :check_existing_events
private
...
def check_existing_events
raise Exception.new("Events exist that use that status.") unless Event.where(:status_id => self.id).count == 0
end
end
class StatusesController < ApplicationController
...
def destroy
@status = Status.find(params[:id])
begin
@status.destroy
rescue Exception => e
flash[:error] = e.message
redirect_to statuses_url
return
end
respond_to do |format|
format.html { redirect_to statuses_url }
format.json { head :no_content }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment