ruby/rails相当于javascript组件?
我有一些内容(html)正被编码为此javascript的结果(来自),并发送到我的rails应用程序:ruby/rails相当于javascript组件?,javascript,ruby-on-rails,ruby,character-encoding,Javascript,Ruby On Rails,Ruby,Character Encoding,我有一些内容(html)正被编码为此javascript的结果(来自),并发送到我的rails应用程序: function encode_utf8_b64(string) { return window.btoa(unescape(encodeURIComponent(string))); } 使其恢复原样的对应js代码如下: function decode_utf8_b64(string) { return decodeURIComponent(escape(window.atob(stri
function encode_utf8_b64(string) {
return window.btoa(unescape(encodeURIComponent(string)));
}
使其恢复原样的对应js代码如下:
function decode_utf8_b64(string) {
return decodeURIComponent(escape(window.atob(string)));
}
我的问题是,ruby中是否有decodeURIComponent()的等价物?到目前为止,我已经有了这个解决方案,但我错过了decodeURIComponent的最后一步:
CGI::escape(Base64.decode64(string))
可能有助于:
def decode_utf8_b64(string)
URI.unescape(CGI::escape(Base64.decode64(string)))
end
您还必须添加必要的rubygem:
require 'uri'
我已经在ruby 1.9.2上对此进行了测试。谢谢-当我应用URI.unescape或URI.decode时,我得到一个“UTF-8中的无效字节序列”错误。似乎CGI::escape()的输出是US-ASCII。当我尝试在应用URI.unescape之前使用Iconv将字符串转换为UTF-8时,它停止抛出错误,但似乎只是删除了一个解码步骤,因此输出仍然是一团混乱。有什么建议吗?或者这是另一个问题。