如何使用 Ruby 2.7.0 修复 Rails 的警告消息

Nez*_*zir 55 ruby warnings ruby-on-rails ruby-on-rails-6 ruby-2.7

有没有人解决这个问题Ruby 2.7.0

我使用rbenv并安装了 Ruby v2.7.0,然后使用Rails v6.0.2.1.

目前,通过运行其中之一

rails s
rails s -u puma
rails s -u webrick
Run Code Online (Sandbox Code Playgroud)

服务器已启动并且站点已提供服务,但在Console日志中我看到两条警告消息:

local:~/rcode/rb27$ rails s
=> Booting Puma
=> Rails 6.0.2.1 application starting in development 
=> Run `rails server --help` for more startup options
.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Puma starting in single mode...
* Version 4.3.1 (ruby 2.7.0-p0), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
* Listening on tcp://[::1]:3000 
Run Code Online (Sandbox Code Playgroud)

因此,警告消息是:

**.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call**

**.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/actionpack-6.0.2.1/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here**
Run Code Online (Sandbox Code Playgroud)

Kha*_*oui 70

抑制警告,如:

warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call

现在,只需将RUBYOPT环境变量前缀/传递给您的 rails 命令:

RUBYOPT='-W:no-deprecated -W:no-experimental' rails server
或者
RUBYOPT='-W:no-deprecated -W:no-experimental' rails db:migrate

这可能不适用于早期版本的 ruby​​。

为了与早期版本的 ruby​​ 向后兼容,请在它前面加上前缀RUBYOPT='-W0'

例子:

RUBYOPT='-W0' bundle exec rspec

如果您不想在每次运行命令时都添加前缀,那么只需将其添加到您的.zshrcor的最后一行.bashrc(无论您使用的是什么):

export RUBYOPT='-W:no-deprecated -W:no-experimental'
或者
export RUBYOPT='-W0'

另请参阅此处注释的最后一点:https :
//rubyreferences.github.io/rubychanges/2.7.html#warning-and-

  • 这样做的一个问题是,如果安装了以前版本的 Ruby,其中 -W:no-deprecated' 标志无效,将会抛出错误。我通过使用 export RUBYOPT='-W0' 修复了这个问题 (2认同)

iGE*_*GEL 11

更新到 Rails 6.0.3,他们修复了警告。

如果您仍然收到警告,则是其他库(查看是否有固定版本或提交补丁)或您自己的代码(如何修复)。


Nez*_*zir 5

显然,ruby 团队需要一些时间才能在下一个 ruby​​ 版本中删除所有这些警告。现在你终端中的命令

`RUBYOPT='-W:no-deprecated' rails s` 
Run Code Online (Sandbox Code Playgroud)

在我的基本的、普通的新 Rails 6.0.2.1 && ruby​​ 2.7.0 项目中,删除上面问题中的这两行警告。

另外,用命令

RUBYOPT='-W:no-experimental' rails s
Run Code Online (Sandbox Code Playgroud)

您将隐藏有关实验性功能的警告。

您可以将这两者结合在一个命令中,例如:

RUBYOPT='-W:no-deprecated -W:no-experimental' rails s
Run Code Online (Sandbox Code Playgroud)

但是,我在使用 rails 5.2 和 ruby​​ 2.6.4 构建的旧项目中尝试了这些命令,后来升级到了 rails 6.0.1,但对于我从不同的 rails Active* 模块和 ruby​​ gem 获得的所有警告消息,它们都没有很好地处理。

可能我们需要一些时间来升级代码和 gems 以获得最新的东西。