Ruby on Rails 在 escape_javascript 中处理 UTF-8

发布于 2024-10-30 07:26:26 字数 1227 浏览 1 评论 0原文

我有一个部分 _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

澉约 2024-11-06 07:26:26

自 Ruby192 以来,所有这些编码问题一直令人头疼。尝试这样做,看看它是否有效:

4: $('#searchresults').html('<%= escape_javascript( render("shared/searchresults").force_encoding("utf-8") ) %>');

好的。所以这是一列失败的火车..查看此页面..它帮助我解决了与您正在查看的问题类似的问题..

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:

4: $('#searchresults').html('<%= escape_javascript( render("shared/searchresults").force_encoding("utf-8") ) %>');

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

笙痞 2024-11-06 07:26:26

我终于做到了,不是最漂亮的解决方案,但我必须手动转换我想显示的每个字符串:

def self.encode( string )
    unless string.nil?
        string.encoding == 'UTF-8'? string : string.force_encoding( 'utf-8');
    end
end

thx,2potatocakes 你的链接有帮助

I finally made it, not the prettiest solution, but I had to manually convert every string I wanted to display with:

def self.encode( string )
    unless string.nil?
        string.encoding == 'UTF-8'? string : string.force_encoding( 'utf-8');
    end
end

thx, 2potatocakes your link helped

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文