If you ever have a string that doesn't play nice with URLs, that's probably because it has spaces, or slashes, or anything like that. So, with this easy method, we can turn that string into a URL-friendly string!
def url_encode(str)
str.b.gsub(/[^a-zA-Z0-9_\-.~]/n) { |m| format('%%%<val>02X', val: m.unpack1('C')) }
end
> url_encode('foo/bar')
=> "foo%2Fbar"
> url_encode('hello world')
=> "hello%20world"
> url_encode('foo-bar/hello world')
=> "foo-bar%2Fhello%20world"