Reputation: 5549
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.
Upvotes: 0
Views: 3171
Reputation: 5549
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
Upvotes: 0
Reputation: 2260
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..
Upvotes: 1