Url encodes ruby ​​equivalent on rails

Is there an equivalent PHP urlencode in Ruby on Rails 2.3.5? (It encodes the string to be used in the request part of the URL) I googled this, but all the answers seem to date back to 2006 and seem to be dates. This is what I found . It seems a bit abnormal to call CGI::escape in the view.

Is there an equivalent helper function?

Thank!

+45
ruby php ruby-on-rails
Mar 01 '10 at 3:33
source share
4 answers

I believe the helper method u is what you are looking for:

 <%=u "URL ENCODE <p>ME</p>" %> 

I can not find the documentation for this method, but if I find it in the near future, I will definitely put a link here.

Edit: you can find the documentation for this method here .

+96
Mar 01 '10 at 3:36
source share

If you want to do this without ERB, you can use the following:

 Rack::Utils.escape('http://example.com') #=> "http%3A%2F%2Fexample.com" 
+60
Jul 09 '12 at 5:12
source share

This worked better for me than Rack::Utils.escape :

 URI::escape('http://example.com/?param=Hello World') 

Since it replaced the spaces %20 instead of +

+38
Nov 11 '12 at 3:16
source share

ERB::Util.html_escape , which is smoothed to h and ERB::Util.url_encode , which is smoothed to u .

http://ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB/Util.html

The names of the methods seem to have changed after Sam Sofes' answer, but they do not have aliases.

+5
Oct 23 '13 at 19:36
source share



All Articles