0

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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Apologies DeX3, Try the above link instead. Let me know if it works
It worked for me, but I had to force the encoding for the output of render: (render("shared/searchresults")).force_encoding("utf-8")
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.