Forum: Ruby on Rails Ajax calls and characters encoding (accents)

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.
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-05-09 15:03
Hello, my application controller defines

before_filter :set_charset
def set_charset
   @headers["Content-Type"] = "text/html; charset=ISO-8859-1"
end

Everything is fine and accents are rendered correctly in the browser.

However, when I call a controller/action from Ajax (Prototype), the
charset is not taken in account and accents are garbage displayed ('?'
sign)

I've tried to put
@headers["Content-Type"] = "text/html; charset=ISO-8859-1"
in the partial view rendered from the ajax call
and/or
in the the controller code itself

No way, still can't have correctly rendered accented chars

How can I do ?

I've found a thread in the forum stating that Prototype uses only utf-8
? I certainly don't want to migrate to utf-8 now. Is it true ?

Thanks
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 steve ross (Guest)
on 2006-05-09 18:48
(Received via mailing list)
Here's my code:

def set_charset
  if request.xhr?
    @headers["Content-Type"] = "text/javascript; charset=utf-8"
  else
    @headers["Content-Type"] = "text/html; charset=utf-8"
  end
end

HTH
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-05-09 19:22
yes Steve, but the problem is that I don't want to switch to utf-8 now.
In fact, I even did a try without any response header BUT just with the
strings utf-8 encoded by RadRails and it worked out of the box.

I tried with encoding special characters as html entities. It works when
used directly into the partial's view but not when the string is
returned by a ruby method.

eg

1) model
class ContactEvent < ActiveRecord::Base
  def self.evt_modes
    return {'TEL' => 'T&eacute;l&eacute;phone', 'RDV' => 'Rendez-vous',
'MISC' => 'Autre'}
  end
end

2) partial
This is a special char '&eacute;'
Mode <%= select 'contact_event', 'how', ContactEvent.evt_modes.invert %>

When rendered, the &eacute; is rendered correctly in "This is a special
char" but not at all in the items on the select list. I really don't
understand why.


Any idea ??
5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 steve ross (Guest)
on 2006-05-09 20:21
(Received via mailing list)
A different facet of this issue is giving me trouble too. I'm not
convinced I completely understand it.

Questions:

1. Have you tried calling the Ajax method via GET? (http://myserver/
mycontroller/myhandler)? What were the results?

2. Have you tried GET via telnet to get the actual header info to see
what the browser might be reacting improperly to?

3. Are you convinced that the character codes coming from the partial
should render correctly in the characterset you've declared? You can use
logger.debug to hex dump the codes of the offending characters.

Note: IE/Win does heuristic analysis of characters in the absence of
headers and will try to figure out the right character set. If this is
breaking you, try Moz or Opera to see if the problem repros.

You'd think after this long, the software development world (emphasis on
WORLD) would be able to figure out some happy way to make characters
render reliable across disparate viewing platforms.
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-05-10 10:49
steve ross wrote:
> A different facet of this issue is giving me trouble too. I'm not
> convinced I completely understand it.
>
> Questions:
>
> 1. Have you tried calling the Ajax method via GET? (http://myserver/
> mycontroller/myhandler)? What were the results?

Got it ! The error is due to the fact that &eacute; is transformed to
&amp;eacute;

But why ? And how to change that ? Is it a bug ?
This topic is locked and can not be replied to.