0

In my Rails application I need to convert Strings like

Friedrichtraße 123, Berlin, Germany

into URLs like

Friedrichstra%C3%9Fe+123,+Berlin,+Germany

How can this be done in Ruby?

Thanks for any help.

Tintin81
  • 9,292
  • 19
  • 74
  • 157

3 Answers3

3
require 'uri'

URI::encode("Friedrichtraße 123, Berlin, Germany")
#=> "Friedrichtra%C3%9Fe%20123,%20Berlin,%20Germany"
Patrick Oscity
  • 51,870
  • 16
  • 134
  • 161
adbeel
  • 374
  • 1
  • 9
3
require 'cgi'

CGI.escape('Friedrichtraße 123, Berlin, Germany')
# => "Friedrichtra%C3%9Fe+123%2C+Berlin%2C+Germany"
Arup Rakshit
  • 113,563
  • 27
  • 250
  • 306
1

To the two sub-Rails answers I will add: Invoke rake routes, read in the first column the helper prefix of the URL you want - let's call it home - then pack your variables as hash options into home_path:

home_path(:address => 'Friedrichtraße 123, Berlin, Germany')
Phlip
  • 5,392
  • 3
  • 29
  • 46