Forum: Ruby on Rails RJS & Unicode (again)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dave (Guest)
on 2006-03-14 15:57
I have a problem with unicode & RJS. I searched several boards & blogs,
but was not able to find a solution. Let me explain my problem with an
example.

View (test.rhtml):
  ...
  <div id="a_test_div">äüö������</div>
  ...

Output:
äüö������

RJS-Template (an_action.rjs):
  page.replace_html 'a_test_div', 'äüö������'


Calling 'an_action' changes the content of 'a_test_div', the output
should remain the same, but Instead of 'äüö������' i get
'�¤�¼�¶������������' which seems to be an encoding problem.

I've found a workaround for strings (introduced by Thomas F.) by
using
  page.replace_html 'messageEditor', 
'äüö������'.gsub(/([^\x00-\x9f])/u)
{ |s| "&#x%x;" % $1.unpack('U')[0] }
instead of
  page.replace_html 'a_test_div', 'äüö������'
but this strategy of course fails while rendering partials.

Are there any better suggestions?

Thanks
Dave
Chris S. (Guest)
on 2006-03-14 15:59
I'm having the same problem too.
Dave (Guest)
on 2006-03-14 16:07
Sorry, I forgot:

A permanent solution can be reached by changing 'render' method in
'add_rjs_to_javascript_helper.rb' to:

def render(*options_for_render)
  (Hash === options_for_render.first ?
    @context.render(*options_for_render) :
     options_for_render.first.to_s).
       gsub(/([^\x00-\x9f])/u) { |s| "&#x%x;" % $1.unpack('U')[0] }
end

but I'm still not shure, if it is really a good one.

Better suggestions are always welcome.
This topic is locked and can not be replied to.