仅在URL中编码空格和逗号

时间:2013-07-26 16:20:10

标签: ruby-on-rails ruby

我正在尝试在rails中编码用于图像附件的url,但是使用CGI :: escape或URI.escape似乎编码所有内容。我只需要编码的逗号和空格,没有别的。我将如何在铁轨中做到这一点?我做了gsub,但只能替换逗号或空格,而不是两者。有办法做到这两点吗?

http://"URL"?operation=getfieldclip&outlinePoints=600%2C600%2C400%2CTest Area%2C44.982643%2C-94.696723%2C44.982343%2C-94.696723%2C44.982293%2C-94.697170%2C44.982293%2C-94.697555%2C44.982313%2C-94.697740%2C44.982333%2C-94.697987%2C44.982363%2C-94.698110%2C44.982403%2C-94.698233%2C44.982453%2C-94.698341%2C44.982493%2C-94.698511%2C44.982553

我可以更改逗号,但不确定如何同时更改空间。

1 个答案:

答案 0 :(得分:2)

您应该使用CGI.escape。不要将它用于整个URL,而只用于它的值。

require "cgi"
"http://URL?" + params.map{|k, v| "#{k}=#{CGI.escape(v)}"}.join("&")