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,821
  • 20
  • 85
  • 178

3 Answers3

3
require 'uri'

URI::encode("Friedrichtraße 123, Berlin, Germany")
#=> "Friedrichtra%C3%9Fe%20123,%20Berlin,%20Germany"
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
adbeel
  • 384
  • 1
  • 9
3
require 'cgi'

CGI.escape('Friedrichtraße 123, Berlin, Germany')
# => "Friedrichtra%C3%9Fe+123%2C+Berlin%2C+Germany"
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
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,253
  • 5
  • 32
  • 48