我贡献的一个 Rails 3.2 应用程序有一个方法可以将用户输入强制转换为 UTF-8。
require "iconv"
def normalize(user_input_text)
Iconv.new('UTF-8//IGNORE', 'UTF-8').iconv(user_input_text.dup)
end
它基本上以 UTF-8 编码字符串并忽略无法转码的字符。
但不是所有通过表单进入 Rails 的用户数据都将采用 UTF-8 编码吗?
也就是说,这段代码是不是似是而非,没有必要?
最佳答案
这些资源表明您确实是对的。
Now that the vast majority of web input is UTF-8, we set the inbound parameters to UTF-8. This will eliminate many cases of incompatible encodings between ASCII-8BIT and UTF-8.
https://github.com/rails/rails/commit/25215d7285db10e2c04d903f251b791342e4dd6a
Rails 3 solves this very nicely by doing a number of things including interpreting params as UTF-8 and adding workarounds for Internet Explorer
关于ruby-on-rails - 来自 Rails 表单的用户数据不是采用 UTF-8 编码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14883184/