url encode equivalent in ruby on rails

PhpRuby on-RailsRuby

Php Problem Overview


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!

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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

Rack::Utils.escape('http://example.com')
#=> "http%3A%2F%2Fexample.com"

Solution 3 - Php

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 +

Solution 4 - Php

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

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

The method names seem to have changed since Sam Soffes answer, but the aliases haven't.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYuval KarmiView Question on Stackoverflow
Solution 1 - PhpMike TrpcicView Answer on Stackoverflow
Solution 2 - PhpSam SoffesView Answer on Stackoverflow
Solution 3 - PhpyorchView Answer on Stackoverflow
Solution 4 - PhpwebdevguyView Answer on Stackoverflow