在Rails中沉默弃用警告3

任何人都可以告诉我如何在Rails 3中抑制弃用警告?

我有一些情况下它会抛出误报。 即使用 – 来自haml的for循环和来自dynamic_form插件的f.error_messages。

谢谢

要使所有弃用警告静音,您可以执行以下操作:

ActiveSupport::Deprecation.silenced = true 

这可以放在初始化程序或特定环境的环境文件中(例如,仅在生产中静音)。

或者对于特定代码段,将其包含在块中:

 ActiveSupport::Deprecation.silence do # no warnings for any use of deprecated methods here end 

这适用于Rails 3和4。

使用Rails 3.2.12,接受的答案对我不起作用。 将它放在环境/ production.rb或初始化程序中仍然会输出警告。 在初始化应用程序之前,我必须将它放在我的config / environment.rb文件中:

 # Load the rails application require File.expand_path('../application', __FILE__) ::ActiveSupport::Deprecation.silenced = true if Rails.env.production? # Initialize the rails application Notices::Application.initialize! 

Ryan Daigle写了一篇关于此的文章,其中他还展示了如何拦截弃用警告并对其执行其他操作,例如将其发送到日志文件:

 ActiveSupport::Deprecation.behavior = Proc.new { |msg, stack| MyLogger.warn(msg) } 

http://ryandaigle.com/articles/2006/12/4/how-to-turn-deprecation-warnings-off-in-rails