我正在使用rails3,我正在查找显示404.html的所有例外列表以及将在PRODUCTION模式下映射到500.html的例外列表。
现在我需要添加类似
的内容rescue_from ActiveRecord::RecordNotFound, :with => :render_404
在我的application_controller中,我不喜欢它。我认为Rails应该自动处理它。
答案 0 :(得分:2)
我在我的应用程序控制器中执行此操作:
rescue_from Exception, :with => :handle_error
def handle_error(exception)
if exceptions_to_treat_as_404.include?(exception.class)
render_404
else
raise exception if Rails.env == 'development'
body = exception_error_message(exception)
#to logger
logger.fatal( body )
#to email
from = '<errors@peakdemocracy.com>'
recipients = "<robert@peakdemocracy.com>"
subject = "[ERROR] (#{exception.class}) #{exception.message.inspect}"
GenericEmail.create(:subject => subject, :from => from, :recipients => recipients, :body => body)
#to PageRequest table
log_request(true)
#render error message
render_500
end
end
def exceptions_to_treat_as_404
exceptions = [AbstractController::ActionNotFound,
ActiveRecord::RecordNotFound,
ActionController::UnknownController,
URI::InvalidURIError,
ActionController::UnknownAction]
exceptions << ActionController::RoutingError if ActionController.const_defined?(:RoutingError)
exceptions
end
答案 1 :(得分:0)