0

我有以下代码在生产中有效:

redirect_to "/mycontroller/index.html#&panel1-2?error=会员 ID 和/或出生日期无效"

在开发中(使用 Webrick),我收到以下错误消息:

ERROR URI::InvalidURIError: bad URI(is not URI?): http://localhost:3000/mycontroller/index.html#&panel1-2?error=Invalid Member ID and/or Date of Birth

但是,如果我在浏览器地址栏中复制并粘贴所谓的错误 URI,它就可以工作!

我尝试了错误消息文本的各种组合,并且单词之间的空格导致了它。我可以通过使用 URI.escape 或其他一些技术来转义空格来使其工作,但是我必须在代码中更改数百个这样的事件,这些都可以正常工作。

感谢您花时间提供帮助。

4

2 回答 2

3

如下图,你的 URI 实际上是无效的。您的 URI 中不能有未转义的空格。

1.9.3p194 :001 > require 'URI'                                                                                                                             => true
1.9.3p194 :002 > URI.parse('http://localhost:3000/mycontroller/index.html#&panel1-2?error=Invalid Member ID and/or Date of Birth')
URI::InvalidURIError: bad URI(is not URI?): http://localhost:3000/mycontroller/index.html#&panel1-2?error=Invalid Member ID and/or Date of Birth
    from /Users/ccashwell/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/common.rb:176:in `split'
    from /Users/ccashwell/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/common.rb:211:in `parse'
    from /Users/ccashwell/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/uri/common.rb:747:in `parse'
    from (irb):2
    from /Users/ccashwell/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'

您应该escape使用 URI:

1.9.3p194 :003 > URI.escape('http://localhost:3000/mycontroller/index.html#&panel1-2?error=Invalid Member ID and/or Date of Birth')
 => "http://localhost:3000/mycontroller/index.html%23&panel1-2?error=Invalid%20Member%20ID%20and/or%20Date%20of%20Birth"

然后你可以redirect_to转义的URI:

redirect_to URI.escape('http://localhost:3000/mycontroller/index.html#&panel1-2?error=Invalid Member ID and/or Date of Birth')
于 2013-08-12T23:09:06.077 回答
0

空格通常替换%20为 url。大多数网络浏览器会自动为您替换它,因此这解释了为什么浏览器可以访问该站点。只需使用%20代替您的空间,您就应该准备就绪。

于 2013-08-12T22:39:07.400 回答