admin管理员组文章数量:1356120
We had a Zero Width Space character problem with our rails app. Somebody copied and pasted a configuration value (a URL) into a form in our rails app, which later caused confusing error messages. It turned out to be caused by the pasted string having a leading Zero Width Space character, preventing it matching other strings as expected. Evil.
We have a large rails app with lots of form fields (ActiveAdmin), so my question is... is there an elegant/recommended way of always stripping away Zero Width Space characters, so that we never hit such an issue again? Should I try to apply such a fix across all form fields?
I'm thinking of adding a method:
def strip_zero_width_chars
attributes.each do |attr, value|
if value.is_a?(String)
self[attr] = value.strip.gsub(/[\u200B\u200C\u200D\uFEFF]/, '')
end
end
end
And applying that to my config model with before_save :strip_zero_width_chars
, to solve it for this particular form.
But I'm not sure if there's a better more elegant way (Side note: I was a bit surprised that ruby strip
doesn't get rid of zero width spaces)
And my instinct is to stop short of making this fix apply to all models throughout the app, for fear of unintended consequences. Can anyone offer any wisdom/experience either way?
本文标签: Rails way to elegantly avoid Zero Width Space character problemsStack Overflow
版权声明:本文标题:Rails way to elegantly avoid Zero Width Space character problems - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744043332a2581069.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论