Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

url encode equivalent in ruby on rails

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

Is there an equivalent helper function?

Thanks!

like image 388
Yuval Karmi Avatar asked Mar 01 '10 03:03

Yuval Karmi


3 Answers

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

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

I can't seem to find the documentation for that method, but if I find it in the near future I'll be sure to put a link in here.

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

like image 172
Mike Trpcic Avatar answered Oct 13 '22 17:10

Mike Trpcic


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

Rack::Utils.escape('http://example.com')
#=> "http%3A%2F%2Fexample.com"
like image 26
Sam Soffes Avatar answered Oct 13 '22 17:10

Sam Soffes


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

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

Because it replaced the spaces with %20 instead of +

like image 40
yorch Avatar answered Oct 13 '22 17:10

yorch