消除 Rails 3 中的弃用警告

发布于 2024-08-30 07:52:11 字数 119 浏览 8 评论 0原文

谁能告诉我如何消除 Rails 3 中的弃用警告?

我遇到过一些会引发误报的情况。即在 haml 和来自dynamic_form插件的f.error_messages中使用-for循环。

谢谢

Can anyone tell me how to silence deprecation warinings in Rails 3?

I have a few situations where it is throwing false positives. Namely using - for loops in haml and f.error_messages from the dynamic_form plugin.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野生奥特曼 2024-09-06 07:52:11

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

ActiveSupport::Deprecation.silenced = true

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

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

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

这适用于 Rails 3 和 Rails 3。 4.

To silence all deprecation warnings you can do:

ActiveSupport::Deprecation.silenced = true

This could be placed in an initializer or in the environment file for a specific environment (e.g. to silence only in production for example.)

Or for a specific section of code, enclose it in a block:

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

This works for both Rails 3 & 4.

嘦怹 2024-09-06 07:52:11

接受的答案对 Rails 3.2.12 不起作用。将其放置在环境/生产.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!

The accepted answer didn't work for me with Rails 3.2.12. Placing it in either the environments/production.rb or an initializer still outputted the warnings. I had to put it in my config/environment.rb file before the application was initialized:

# 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!
街角卖回忆 2024-09-06 07:52:11

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

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

http://ryandaigle.com/articles/2006/12/4/how-to-turn-弃用警告-off-in-rails

Ryan Daigle wrote an article about this, in which he also showed how you can intercept the deprecation warning and do something else with it, like send it to a log file:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文