在Rails中沉默弃用警告3

26 ruby warnings ruby-on-rails deprecated ruby-on-rails-3

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

我有一些情况下它会抛出误报.即在dynamic_form插件中使用 - for ha循环中的for循环和f.error_messages.

谢谢

mik*_*kej 55

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

ActiveSupport::Deprecation.silenced = true
Run Code Online (Sandbox Code Playgroud)

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

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

ActiveSupport::Deprecation.silence do
  # no warnings for any use of deprecated methods here
end
Run Code Online (Sandbox Code Playgroud)

这适用于Rails 3和4.

  • 我也有一些很棒的黑色胶带,你可以放在发动机油灯上;) (7认同)

spy*_*yle 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!
Run Code Online (Sandbox Code Playgroud)


bjn*_*ord 7

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

ActiveSupport::Deprecation.behavior = Proc.new { |msg, stack| MyLogger.warn(msg) }
Run Code Online (Sandbox Code Playgroud)

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