我需要将 'jonathan-morón-16394669' 转换为 'jonathan-mor%C3%B3n-16394669'

I need to convert 'jonathan-morón-16394669' into 'jonathan-mor%C3%B3n-16394669'

我正在尝试解决一个问题,因为有时会保存 'jonathan-mor%C3%B3n-16394669' 等数据,有时会保存 'jonathan-morón-16394669' 等数据。因此,我需要一种平滑的方式在这两个版本的字符串之间移动,以便能够比较事物并通过匹配它们来意识到它是相同的,并且只是继续进行而不在数据库中保存两次。

我知道我可以开始使用类似这样的东西,但相反:

line = line.replace("%C3%A9","e")
line = line.replace("%C3%B3","o")
line = line.replace("%E2%80%93","-")
line = line.replace("%C3%A6","e")
line = line.replace("%C3%A8","e")
line = line.replace("%C3%A0","e")
line = line.replace("%C3%A1","i")
line = line.replace("%C5%82","l")
line = line.replace("%C5%84","n")
line = line.replace("%C3%BF", "y")
line = line.replace("%C3%BE", "p")
line = line.replace("%C3%BD", "y")
line = line.replace("%C3%BC", "u")
line = line.replace("%C3%BB", "u")
line = line.replace("%C3%BA", "u")
line = line.replace("%C3%B9", "o")
line = line.replace("%C3%B6", "o")
line = line.replace("%C3%B5", "o")
line = line.replace("%C3%B4", "o")
line = line.replace("%C3%B3", "o")
line = line.replace("%C3%B2", "o")
line = line.replace("%C3%B1", "n")
line = line.replace("%C3%B0", "e")
line = line.replace("%C3%AC", "i")
line = line.replace("%C3%AD", "i")
line = line.replace("%C3%AE", "i")
line = line.replace("%C3%AF", "i")
line = line.replace("%C3%81","A")
line = line.replace("%C3%82","A")
line = line.replace("%C3%83","A")
line = line.replace("%C3%84","A")
line = line.replace("%C3%85","A")
line = line.replace("%C3%86","AE")
line = line.replace("%C3%87","C")
line = line.replace("%C3%88","E")
line = line.replace("%C3%89","E")
line = line.replace("%C3%8A","E")
line = line.replace("%C3%8B","E")
line = line.replace("%C3%8C","I")
line = line.replace("%C3%8D","I")
line = line.replace("%C3%8E","I")
line = line.replace("%C3%8F","I")
line = line.replace("%C3%90","D")
line = line.replace("%C3%91","N")
line = line.replace("%C3%92","O")
line = line.replace("%C3%93","O")
line = line.replace("%C3%94","O")
line = line.replace("%C3%95","O")
line = line.replace("%C3%96","O")
line = line.replace("%C3%98","O")
line = line.replace("%C3%99","U")
line = line.replace("%C3%9A","U")
line = line.replace("%C3%9B","U")
line = line.replace("%C3%9C","U")
line = line.replace("%C3%9D","Y")
line = line.replace("%C3%9F","B")
line = line.replace("%C3%a0","a")
line = line.replace("%C3%a1","a")
line = line.replace("%C3%a2","a")
line = line.replace("%C3%a3","a")
...

但我需要一个更Rails的解决方案,优雅、简单

感谢 razvans!我需要的答案是

URI.encode("jonathan-morón-16394669")

输出

[19] pry(main)> URI.encode("jonathan-morón-16394669")
=> "jonathan-mor%C3%B3n-16394669"

您可以使用CGI::escape

CGI::escape('jonathan-morón-16394669') # => jonathan-mor%C3%B3n-16394669

你也可以反过来CGI::unescape

CGI::unescape('jonathan-mor%C3%B3n-16394669') # => jonathan-morón-16394669