2010-04-22 42 views
24

任何人都可以告訴我如何沉默Rails 3中的棄用警告?沉默在Rails 3中的棄用警告3

我有幾種情況,它是拋出誤報。即在dynamic_form插件中使用haml和f.error_messages中的for循環。

感謝

+0

是否要在選定的代碼塊中使所有**棄用警告或僅警告? – mikej 2010-04-22 09:52:39

+0

最好只是我知道的代碼段是安全的,但無論哪種方式,如果我可以切換它只是爲了削減一些日誌噪音。 – sfusion 2010-04-22 12:46:02

回答

54

要關閉所有廢棄警告,你可以這樣做:

ActiveSupport::Deprecation.silenced = true 

這可以被放置在初始化或者在環境中的文件在特定的環境(例如,在生產中,例如只有沉默。)

或爲特定的代碼段,它括在塊:

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

這適用於兩個軌道3 & 4.

+0

既工作又好吃,謝謝:) – sfusion 2010-04-22 13:44:10

+6

我也有一些很棒的黑色膠帶可以放在你的發動機機油燈上;) – 2013-12-19 01:22:43

11

接受的答案並沒有爲我使用Rails 3.2.12工作。將其放置在environments/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! 
+0

感謝你發帖,相比於其他的解決方案,這是唯一能在Rails裏工作的東西4 – 2014-03-13 18:40:36