我正在尝试将我的应用程序配置为从我的应用程序中的任何 ActiveRecord 对象打印错误。这样我就可以避免在所有事务中为每个对象打印错误消息。有没有办法做到这一点?
需要这样的东西
在配置/初始化程序中
ActiveRecord::Base.class_eval do
# For each active_record object in my app
if self.errors.any?
puts self.errors.inspect
end
end
最佳答案
我相信您希望将所有 AR 错误集中在一个地方,
一种方法是使用 rescue_from
.简单地说,你可以在你的应用程序 Controller 中做这样的事情
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
#your normal code
rescue_from Exception do |exeception|
#check if this is a AR exception
#if yes then
#log it in a different log file
#if not
# yield (to follow the default exception chain)
end
end
更合适的方法是将它放在一个模块中和
include
它在 ApplciationController
.高温高压
关于ruby-on-rails - 如何在 Rails3 的 config/initializers 下捕获所有 ActiveRecord::Errors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22626819/