在Rails中沉默弃用警告3

时间:2010-04-22 08:53:32

标签: ruby-on-rails ruby ruby-on-rails-3 warnings deprecated

有谁能告诉我如何在Rails 3中压制弃用警告?

我有几种情况会误报误报。即使用 - 来自haml的for循环和来自dynamic_form插件的f.error_messages。

由于

3 个答案:

答案 0 :(得分:55)

要隐藏所有弃用警告,您可以这样做:

ActiveSupport::Deprecation.silenced = true

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

或者对于特定的代码部分,将其括在一个块中:

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

这适用于Rails 3& 4。

答案 1 :(得分:12)

对于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!

答案 2 :(得分:7)

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