Assuming all your controllers inherit from ApplicationController, you can use rescue_from
in your ApplicationController to rescue any errors in any controller.
ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound do |exception|
message = "Couldn't find a record."
redirect_to no_record_url, info: message
end
end
You can have multiple rescue_from
clauses for different error classes, but note that they're invoked in reverse order, so a generic rescue_from
should be listed before others...
ApplicationController < ActionController::Base
rescue_from do |exception|
message = "some unspecified error"
redirect_to rescue_message_url, info: message
end
rescue_from ActiveRecord::RecordNotFound do |exception|
message = "Couldn't find a record."
redirect_to rescue_message_url, info: message
end
end