84

Given the string:

"Hello there world"

how can I create a URL-encoded string like this:

"Hello%20there%20world"

I would also like to know what to do if the string has other symbols too, like:

"hello there: world, how are you"

What would is the easiest way to do so? I was going to parse and then build some code for that.

4 Answers 4

139

In 2019, URI.encode is obsolete and should not be used.


require 'uri'

URI.encode("Hello there world")
#=> "Hello%20there%20world"
URI.encode("hello there: world, how are you")
#=> "hello%20there:%20world,%20how%20are%20you"

URI.decode("Hello%20there%20world")
#=> "Hello there world"
Sign up to request clarification or add additional context in comments.

4 Comments

If you also want to encode dots: URI.encode('api.example.com', /\W/)
Doesn't work, e.g. URI.encode("http://google.com") => "http://google.com". Better use CGI.escape ("https%3A%2F%2Fgoogle.com")
undefined method `encode' for module URI
Read the first line "In 2019, URI.encode is obsolete and should not be used." and check other answers
36

While the current answer says to utilize URI.encode that has been deprecated and obsolete since Ruby 1.9.2. It is better to utilize CGI.escape or ERB::Util.url_encode.

1 Comment

This answer is correct. CGI.escape("www.foo.com/more/here") works now. Key difference is slash's "/", semicolons ":" and array notation "[" "]" started encoding. Thanks!
22

If anyone is interested, the newest way to do this is doing in ERB:

    <%= u "Hello World !" %>

This will render:

Hello%20World%20%21

u is short for url_encode

You can find the docs here

Comments

21

Ruby's URI is useful for this. You can build the entire URL programmatically and add the query parameters using that class, and it'll handle the encoding for you:

require 'uri'

uri = URI.parse('http://foo.com')
uri.query = URI.encode_www_form(
  's' => "Hello there world"
)
uri.to_s # => "http://foo.com?s=Hello+there+world"

The examples are useful:

URI.encode_www_form([["q", "ruby"], ["lang", "en"]])
#=> "q=ruby&lang=en"
URI.encode_www_form("q" => "ruby", "lang" => "en")
#=> "q=ruby&lang=en"
URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en")
#=> "q=ruby&q=perl&lang=en"
URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]])
#=> "q=ruby&q=perl&lang=en"

These links might also be useful:

3 Comments

How can i embed the require 'uri' into the html.erb? Or must I put it into the controller?
The right thing to do, any time there is more than trivial logic needed, is to do all the "computin'" in the controller.
Cool. When should we be using helpers? And what if we do computing that would be used in many places in the helper and include for the controllers. Does it matter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.