Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.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.escape以避免Lint/UriEscapeUnescape警告?_Ruby On Rails_Ruby_Urlencode_Lint_Rubocop - Fatal编程技术网

Ruby on rails 替换URI.escape以避免Lint/UriEscapeUnescape警告?

Ruby on rails 替换URI.escape以避免Lint/UriEscapeUnescape警告?,ruby-on-rails,ruby,urlencode,lint,rubocop,Ruby On Rails,Ruby,Urlencode,Lint,Rubocop,不确定如何修复Rubocop的警告 尝试用CGI替换URI,认为这是“临时”替换,但这破坏了测试套件 下面是使用URI的代码行后面的错误: app/models/media_file.rb:76:5: W: Lint/UriEscapeUnescape: URI.escape method is obsolete and should not be used. Instead, use CGI.escape, URI.encode_www_form or URI.encode_www_form_

不确定如何修复Rubocop的警告

尝试用
CGI
替换
URI
,认为这是“临时”替换,但这破坏了测试套件

下面是使用
URI
的代码行后面的错误:

app/models/media_file.rb:76:5: W: Lint/UriEscapeUnescape: URI.escape method is obsolete and should not be used. Instead, use CGI.escape, URI.encode_www_form or URI.encode_www_form_component depending on your specific use case.
    URI ...
    ^^^

    # app/models/media_file.rb
    ...
    def cdn_url(format: nil)
    if format.nil?
      "#{s3_config.cloudfront_endpoint}/#{escape_url(key)}"
    elsif converted_urls.with_indifferent_access[format.to_s]
      filename = converted_urls.with_indifferent_access[format.to_s]
      if URI.parse(escape_url(filename)).host
        filename
      else
        "#{s3_config.cloudfront_endpoint}/#{escape_url(filename)}"
      end
    else
      converted(url)
    end
  end
...
  private

  def escape_url(url)
    URI
      .escape(url)
      .gsub(/\(/, '%28')
      .gsub(/\)/, '%29')
      .gsub(/\[/, '%5B')
      .gsub(/\]/, '%5D')
  end
编辑:添加使用
URI
CGI
转义的字符串的示例输出:

            url: images/medium/test-image.jpg
URI.escape(url): images/medium/test-image.jpg
CGI.escape(url): images%2Fmedium%2Ftest-image.jpg

            url: images/medium/test-image.jpg
URI.escape(url): images/medium/test-image.jpg
CGI.escape(url): images%2Fmedium%2Ftest-image.jpg

似乎
CGI
并不是
URI
的替代品,因为您可能会相信列表错误。想法?

遇到同样的问题,使用lib修复

escape\u query=URI.escape(搜索、,
Regexp.new(“[^#{URI::PATTERN::UNRESERVED}]”)
#W:Lint/UriEscapeUnescape:URI.escape方法已过时,不应使用。相反,根据您的具体用例,使用CGI.escape、URI.encode\u www\u form或URI.encode\u www\u form\u组件。
解决者:

  • gem
    可寻址
    gem在
    gempec
    Gemfile
  • 需要
    可寻址/uri

  • 添加适当的


为什么你的考试不及格?你测试什么URL,你期望什么输出,它返回什么?cop清楚地告诉你该怎么做。如果
CGI.escape
导致错误,则需要修复这些错误。我们无法帮助您,因为您没有包含任何输入或它们导致的错误。我添加了一些输出(如上),因此很明显,
URI.escape
CGI.escape
正在做不同的事情……那么,如果
URI.escape
是“过时的”,那么替换是什么呢?我建议阅读。TLDR:
URI.escape
已被删除(这已反映出来),cop正在警告您这一点。替代实现的详细信息留给读者,其中
CGI
是一个可能的示例。
CGI.escape
不是
URI.escape
的替代品。例如:
URI.escape('foo-bar')
给出了
'foo%20bar'
,而
CGI.escape('foo-bar')
给出了
'foo+bar'
。例如,如果您试图转义包含空格的基本身份验证密码,则会出现问题。有一些解决办法,但建议OP在不同的情况下只使用
CGI.escape
,是没有帮助的。
gem 'addressable', '~> 2.7'
escaped_query = Addressable::URI.encode_component(search, Addressable::URI::CharacterClasses::QUERY)