Skip to content

Instantly share code, notes, and snippets.

@jsmestad
Created September 30, 2011 23:31
Show Gist options
  • Select an option

  • Save jsmestad/1255329 to your computer and use it in GitHub Desktop.

Select an option

Save jsmestad/1255329 to your computer and use it in GitHub Desktop.
module MissingRecordHandler
extend ActiveSupport::Concern
included do
# Catch any RecordNotFound errors and route them appropriately
# to the 404 handler.
#
# (see #record_not_found)
# (see #render_404)
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
end
module InstanceMethods
# Helper method to ensure the proper error is always raised.
# This should be called when throwing a not found error instead
# of invoking the error directly.
#
# @returns [ActiveRecord::RecordNotFound]
# (see #render_404)
def record_not_found
raise ActiveRecord::RecordNotFound
end
private
# Handles any ActiveRecord::RecordNotFound errors that may be thrown during
# the request cycle. It will push an entry into the log file and render the
# appropriate response based on the mime type of the request.
#
# @note ensure that if you ever invoke this method you return immediately to
# avoid a possible double render error.
def render_404(exception=nil)
logger.info "Rendering 404 with exception: #{exception.message}" if exception.present?
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404.html", :status => :not_found, :layout => false }
format.json { render :text => 'Not Found', :status => :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment