[Ruby on Rails] URL Encoding (url_encode) in ERB

Tadashi Shigeoka ·  Sat, January 7, 2012

I’ll introduce how to URL encode within ERB in Ruby on Rails.

URL Encoding

To URL encode in ERB, use the ERB::Util.url_encode method.

<%= "http://search.example.com/?q=" + url_encode(" 日本語") %>
#=> http://search.example.com/?q=%E6%97%A5%E6%9C%AC%E8%AA%9E

As a short form of the url_encode method, the u method is also available.

<%= "http://search.example.com/?q=" + u(" 日本語") %>
#=> http://search.example.com/?q=%E6%97%A5%E6%9C%AC%E8%AA%9E

That’s all.

【Reference】

Module: ERB::Util

URL Encoding and Decoding in Ruby on Rails

How to URI Encode in Rails - OneRingToFind

That’s all from the Gemba.