Last active
December 16, 2015 05:59
-
-
Save jbutz/5388316 to your computer and use it in GitHub Desktop.
Ruby on Rails Exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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