gpt4 book ai didi

ruby-on-rails - Rails 和 OpenURI

转载 作者:数据小太阳 更新时间:2023-10-29 06:47:26 27 4
gpt4 key购买 nike

我正在尝试从控制台中的全新 Rails 项目运行以下代码段:

uri = URI.parse("http://25.media.tumblr.com/avatar_279ec8ee3427_64.png")
data = open(uri)

此错误:

TypeError: can't convert URI::HTTP into String
from (irb):24:in `open'
from (irb):24
from /Users/kevin/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/console.rb:44:in `start'
from /Users/kevin/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands/console.rb:8:in `start'
from /Users/kevin/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.4/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

我在 Rails 3.0.4 和 Ruby 1.9.2 上运行。有想法该怎么解决这个吗?谢谢!

最佳答案

open() 将接受字符串和 URI 对象。

io = open("http://...")
io = open(URI.parse("http://..."))

如果不包含open-uri,就会出现您描述的错误。

require 'open-uri'

关于ruby-on-rails - Rails 和 OpenURI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5046153/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com