Search code examples
ruby-on-railscharacter-encodinghtml-escape-characters

Ruby on rails escape unicode characters in search


There is a text saved in database, in the encoded format: for example for German letter

Ü

it stores

Ü

I don't know which method was used to make such a conversion. When executing search, I need to transform character Ü to Ü value, how this can be easily done in ROR? The following doesn't work:

'Ü'.html_safe -> Ü
ERB::Util.html_escape('Ü') -> Ü

Solution

  • Use htmlentities gem.

    To convert from HTML entity to UTF-8 char:

    require 'htmlentities'
    HTMLEntities.new.decode('Ü')  # => "Ü"
    

    From UTF-8 to HTML entity:

    require 'htmlentities'
    HTMLEntities.new.encode("Ü", :named)  # => "Ü"