如何捕获所有的ActiveRecord ::错误在配置/ initializersin Rails3 [英] How to capture all ActiveRecord::Errors under config/initializersin Rails3
问题描述
我想配置我的应用程序从我的应用程序的任何ActiveRecord的对象打印错误。所以,我能够避免打印错误消息,在所有的地方交易,每一个对象。有没有办法做到这一点?
需要somethind这样
在配置/初始化
的ActiveRecord :: Base.class_eval做
#对于我的应用程序的每个active_record对象
如果self.errors.any?
把self.errors.inspect
结束
结束
我相信你想拥有所有的AR误差在一个地方,
一个方法是使用 rescue_from
。只要你可以做这样的事情在你的应用程序控制器
类的ApplicationController< ActionController的::基地
protect_from_forgery有:异常
#your正常code
rescue_from异常做| exeception |
#check如果这是一个的AR例外
#如果是的话
#log在一个不同的日志文件
#如果不
#收益率(按照默认异常链)
结束
结束
更恰当的方法是有这样的一个模块和包括
它 ApplciationController
。
心连心
I am trying to configure my applications to print errors from any ActiveRecord object in my app. So that I can avoid printing the error message for each object in all place of transactions. Is there any way to do this?
Need somethind like this
in config/initializers
ActiveRecord::Base.class_eval do
# For each active_record object in my app
if self.errors.any?
puts self.errors.inspect
end
end
I believe you want to have all the AR errors in one place,
One method would be using rescue_from
. Simply you could do something like this in your application 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
more proper way would be to have this in a module and include
it in ApplciationController
.
HTH
这篇关于如何捕获所有的ActiveRecord ::错误在配置/ initializersin Rails3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!