【问题标题】:No such file or directory @ rb_sysopen for external URL / Rails 6.11 / Ruby 3外部 URL / Rails 6.11 / Ruby 3 没有这样的文件或目录 @ rb_sysopen
【发布时间】:2021-07-15 16:42:54
【问题描述】:

这在 Rails 6 中运行良好,但现在将 Rails 和 Ruby 升级到 6.11 和 3,它触发了这个“经典”错误。简而言之,例如这个直接指向 Firefox 徽标的外部 URL,使用 Rails 控制台:

require 'open-uri'
open("https://brandemia.org/sites/default/files/inline/images/firefox_logo.jpg")
Traceback (most recent call last):
        4: from (irb):1:in `<main>'
        3: from (irb):2:in `rescue in <main>'
        2: from (irb):2:in `open'
        1: from (irb):2:in `initialize'
Errno::ENOENT (No such file or directory @ rb_sysopen - https://brandemia.org/sites/default/files/inline/images/firefox_logo.jpg)

在应用程序中同样的错误。在 Rails 和 Ruby 升级之前(使用 Ruby 2.5.8 和 Rails 6.0.3.1),这一切正常。

【问题讨论】:

  • 欢迎来到 Stack Overflow,@Dario。我不太明白你的问题(你并没有真正说出一个)。您的 Rails 是在执行 open("https...."),还是您的 Rails 应用程序在 brandemia.org.... 运行,而不是服务 firefox_logo.jpg?

标签: ruby


【解决方案1】:

open-uri 曾经(在 Ruby 3.0 之前)用自己的版本覆盖 Kernel#open,该版本还支持从外部 URL 读取,而不是简单地打开本地文件或运行命令。

混合这两个用例是非常危险的,如果不能确保传递的 URL 在任何地方都是安全的(包括使用 Kernel#open 的第三方代码),则可能存在严重漏洞。

因此,这种覆盖Kernel#open 的行为在 Ruby 2.7 中已被弃用,最终在 Ruby 3.0 中被删除。要打开外部 URL,您可以改用以下代码:

URI.open("https://brandemia.org/sites/default/files/inline/images/firefox_logo.jpg")

【讨论】:

  • 没错。我今天下午解决了它,就是这样。感谢您的回答!
  • 很好的解释!这解决了我的问题。谢谢!
【解决方案2】:

我不确定变化来自哪里,但是

require 'open-uri'

uri = 'https://brandemia.org/sites/default/files/inline/images/firefox_logo.jpg'
URI.open(uri) # ! instead of open without URI

应该可以。

更新 open-uri 确实声明了 Kernel#open 直到 3.0 版本。显然,这在 2.7 中已被弃用。从现在开始,您需要拨打URI.open。在此处查看提交:https://github.com/ruby/open-uri/commit/53862fa35887a34a8060aebf2241874240c2986a

【讨论】:

  • 没错。我今天下午解决了它,就是这样。感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 2015-05-11
相关资源
最近更新 更多