Mr_Nizzle
Mr_Nizzle

Reputation: 6714

How to transform text with characters to HTML Entities with Ruby on Rails?

I Want to use an URL to call a method but i need it to be written with HTML Entities:

so if i have http://www.myurl.com/foobar for example using a Ruby on Rails helper i can get something like:

http%3A%2F%2Fwww.myurl.com%2Ffoobar

Upvotes: 0

Views: 592

Answers (2)

z3cko
z3cko

Reputation: 3054

there is the great rubygem "htmlentities" which does the trick: http://htmlentities.rubyforge.org/

Upvotes: 1

theIV
theIV

Reputation: 25774

I don't know if there's anything built directly into rails to do all of that escaping, but if you require 'cgi' you can use CGI::escape.

ruby-1.8.7-p174 :001 > require 'cgi'
 => true 
ruby-1.8.7-p174 :002 > s = "http://www.myurl.com/foobar"
 => "http://www.myurl.com/foobar" 
ruby-1.8.7-p174 :003 > CGI::escape(s)
 => "http%3A%2F%2Fwww.myurl.com%2Ffoobar" 

Obviously, to make it look a little nicer in your views, or wherever, you could wrap that method in a helper.

Upvotes: 3

Related Questions