Question

I have the following code which works in production:

 redirect_to "/mycontroller/index.html#&panel1-2?error=Invalid Member ID and/or Date of Birth"

In development(using Webrick), I am getting the following error message:

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

But if I copy and paste the supposedly bad URI in my browser address bar, it works!

I have tried various combinations of the error message text, and the spaces between words is causing it. I can make it work by using URI.escape or some other technique to escape the spaces, but then I would have to change hundreds of such occurrences in code which is working fine.

Thanks for taking the time to help.

Was it helpful?

Solution

As shown below, your URI is actually invalid. You can't have unescaped spaces in your 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>'

You should escape the 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"

Then you can redirect_to the escaped URI:

redirect_to URI.escape('http://localhost:3000/mycontroller/index.html#&panel1-2?error=Invalid Member ID and/or Date of Birth')

OTHER TIPS

Spaces are usually replaced with %20 for urls. Most web browsers will automatically replace this for you, so this explains why the browser can go to the site. Just use %20 in place of your spaces and you should be all set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top