JPatel
JPatel

Reputation: 3

Encoding only spaces and commas in URL

I am trying to encode a url in rails for an image attachment, but using CGI::escape or URI.escape seems to encode everything. I just need the commas and spaces encoded, nothing else. How would I go about doing that in rails? I did gsub, but could only replace commas or spaces, not both. Is there a way to do both?

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

I can get the commas changed, but not sure how to get the space changed at the same time as well.

Upvotes: 0

Views: 893

Answers (1)

sawa
sawa

Reputation: 168101

You should use CGI.escape. Don't use it for the entire URL, but only for the values of it.

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

Upvotes: 2

Related Questions