Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 无效URI-如何防止,URI::InvalidURIError错误?_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 无效URI-如何防止,URI::InvalidURIError错误?

Ruby on rails 无效URI-如何防止,URI::InvalidURIError错误?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我从延迟的工作中得到了以下信息: [Worker(XXXXXX pid:3720)] Class#XXXXXXX failed with URI::InvalidURIError: bad URI(is not URI?): https://s3.amazonaws.com/cline-local-dev/2/attachments/542/original/mac-os-x[1].jpeg?AWSAccessKeyId=xxxxxxxx&Expires=1295403309&S

我从延迟的工作中得到了以下信息:

[Worker(XXXXXX pid:3720)] Class#XXXXXXX failed with URI::InvalidURIError: bad URI(is not URI?): https://s3.amazonaws.com/cline-local-dev/2/attachments/542/original/mac-os-x[1].jpeg?AWSAccessKeyId=xxxxxxxx&Expires=1295403309&Signature=xxxxxxx%3D - 3 failed attempts
这个URI在我的应用程序中的来源是

在我的用户邮箱中,我执行以下操作:

  @comment.attachments.each do |a|
    attachments[a.attachment_file_name] = open(a.authenticated_url()) {|f| f.read }
  end
然后在我的附件模型中:

  def authenticated_url(style = nil, expires_in = 90.minutes)
    AWS::S3::S3Object.url_for(attachment.path(style || attachment.default_style), attachment.bucket_name, :expires_in => expires_in, :use_ssl => attachment.s3_protocol == 'https')
  end
也就是说,是否有某种类型的URI.encode或解析可以防止一个有效的URI(当我在浏览器中检查URL工作时)在rails 3中出错和终止延迟的_作业

谢谢大家!

Ruby(至少)有两个模块用于处理URI

是标准库的一部分

,是一个独立的宝石,更全面,并声称符合规范

使用任意一个URL解析URL,使用gem的方法修改任何参数,然后在传递之前使用
将其转换为_s
,您应该准备好了

我尝试了“open(URI.parse(URI.encode(a.authenticated_url())”,但这个错误与OpenURI::HTTPError:403有关

如果您通过浏览器导航到该页面并成功,随后通过代码直接访问该页面失败,则很可能缺少cookie或会话状态。您可能需要使用类似的工具,它将在允许您浏览网站的同时保持该状态


编辑:


我尝试了“open(URI.parse(URI.encode)(a.authenticated_url())”“但是OpenURI::HTTPError:403 Forbidden的错误该错误并不意味着URL不好,它意味着该页面对您不可用。感谢Tin Man.。当您“修改任何参数”时,您能解释一下吗。更好的方法是为上面的URL提供一个示例吗?thxsI添加了一个如何使用可寻址的简单示例。除此之外,我认为您需要尝试一下。”阅读文档,确定它如何适合您的用例,并学习gem。它非常好。Ruby的URI目前在参数编码方面有一些成长的困难,因此对URL的这一部分的任何操作都会传递给可寻址的。
require 'addressable/uri'

url = 'http://www.example.com'

uri = Addressable::URI.parse(url)
uri.query_values = {
  :foo => :bar,
  :q   => '"one two"'
}

uri.to_s # => "http://www.example.com?foo=bar&q=%22one%20two%22"