清理URL slugs的Unicode字符串(Ruby / Rails)

时间:2014-04-09 08:41:55

标签: ruby-on-rails ruby unicode sanitization slug

我有UTF-8编码的帖子标题,我宁愿使用slugs中的相应字符显示。一个例子是Amazon Japan's URL here

如何使用Ruby(或Rails)将任意字符串转换为安全URL slug?

(有一些related PHP帖子,但我找不到Ruby。)

1 个答案:

答案 0 :(得分:3)

从阅读here看起来似乎是一个解决方案:

require 'open-uri'
str = "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a".force_encoding('ASCII-8BIT')
puts URI::encode(str)

Here is the documentation for open-uri。和here is some info on utf-8 encoded url schema

编辑:仔细研究了这一点,我注意到编码只是URI.escape的别名documented here。例子来自以下文档:

require 'uri'

enc_uri = URI.escape("http://example.com/?a=\11\15")
p enc_uri
# => "http://example.com/?a=%09%0D"

p URI.unescape(enc_uri)
# => "http://example.com/?a=\t\r"

p URI.escape("@?@!", "!?")
# => "@%3F@%21"

请告诉我这是您要找的?

编辑#2:我很感兴趣并且继续寻找更多,according to the comments ryan bates' railscasts on friendlyid似乎也适用于中文字符。