Ruby on Rails 在 escape_javascript 中处理 UTF-8
我有一个部分 _searchresults.html.erb 必须以 UTF-8 保存。
然后我有一些 javaxript/AJAX 代码来呈现该部分:
<% # encoding: utf-8
%>
stopLoading();
$('#searchresults').html('<%= escape_javascript( render("shared/searchresults") ) %>');
每次我尝试访问相关页面时,我都会得到:
ActionView::Template::Error (invalid byte sequence in UTF-8):
1: <% # encoding: utf-8
2: %>
3: stopLoading();
4: $('#searchresults').html('<%= escape_javascript( render("shared/searchresults") ) %>');
app/views/searches/index.de.js.erb:4:in `_app_views_searches_index_de_js_erb__423966875_35661432__279394272'
我的所有文件都使用 UTF-8 进行编码,并且所有相关的 *.erb 文件都有 # 编码: utf-8
神奇注释。
我能做些什么吗?
编辑:
我现在尝试手动转义js:
def my_js_escape( js )
if( js )
ret = js.force_encoding( 'utf-8' )
ret.gsub!( /\\/u, '\\\\' )
#ret.gsub!( /<\//u, '<\/' ).force_encoding( 'utf-8' )
#ret.gsub!( /"/u, '\\"' ).force_encoding( 'utf-8' )
#ret.gsub!( /'/u, "\\'" ).force_encoding( 'utf-8' )
#/(\\|<\/|\r\n|[\n\r"'])/
return ret
else
''
end
end
Ruby在每次gsub调用上都会给我同样的错误,即使我在所有替换字符串上放置.force_encoding。
I've got a partial _searchresults.html.erb which has to be saved in UTF-8.
Then I've got some javaxript/AJAX code to render that partial:
<% # encoding: utf-8
%>
stopLoading();
$('#searchresults').html('<%= escape_javascript( render("shared/searchresults") ) %>');
Everytime I try to access the related page I get:
ActionView::Template::Error (invalid byte sequence in UTF-8):
1: <% # encoding: utf-8
2: %>
3: stopLoading();
4: $('#searchresults').html('<%= escape_javascript( render("shared/searchresults") ) %>');
app/views/searches/index.de.js.erb:4:in `_app_views_searches_index_de_js_erb__423966875_35661432__279394272'
All of my files are encoded with UTF-8 and all relevant *.erb files have the # encoding: utf-8
magic comment.
Is there anything I can do about this?
EDIT:
I'm now trying to escape the js manually:
def my_js_escape( js )
if( js )
ret = js.force_encoding( 'utf-8' )
ret.gsub!( /\\/u, '\\\\' )
#ret.gsub!( /<\//u, '<\/' ).force_encoding( 'utf-8' )
#ret.gsub!( /"/u, '\\"' ).force_encoding( 'utf-8' )
#ret.gsub!( /'/u, "\\'" ).force_encoding( 'utf-8' )
#/(\\|<\/|\r\n|[\n\r"'])/
return ret
else
''
end
end
Ruby gives me the same error on every gsub call, even if I put .force_encoding on all the replacement strings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
自 Ruby192 以来,所有这些编码问题一直令人头疼。尝试这样做,看看它是否有效:
好的。所以这是一列失败的火车..查看此页面..它帮助我解决了与您正在查看的问题类似的问题..
http://peppyheppy.com/2011/1/20/ ruby-1-9-2-encoding-utf-8-and-rails-invalid-byte-sequence-in-us-ascii
All these encoding issues since Ruby192 have been a pain in the butt.. Try doing this and see if it works:
OK. So that was a fail train.. Check out this page.. It helped me fix a similar problem to the one you're looking at..
http://peppyheppy.com/2011/1/20/ruby-1-9-2-encoding-utf-8-and-rails-invalid-byte-sequence-in-us-ascii
我终于做到了,不是最漂亮的解决方案,但我必须手动转换我想显示的每个字符串:
thx,2potatocakes 你的链接有帮助
I finally made it, not the prettiest solution, but I had to manually convert every string I wanted to display with:
thx, 2potatocakes your link helped