Setting character encoding - do I do it with Rails or lightt


#1

I have a page which validates “tentatively,” because the validator
uses the default character encoding. None was specified apparently,
so it falls back to UTF-8. How and where do I specify the character
encoding?

Pat


#2

Pat

I have a page which validates “tentatively,” because the validator
uses the default character encoding. None was specified apparently,
so it falls back to UTF-8. How and where do I specify the character
encoding?

In layouts/application.html (f.ex), you can add:

...

If you use AJAX and partial rendering, you may have to specify it
through headers, in order for special international characters to render
correctly :

 def update_positions
   @lists = ...
   @headers["Content-Type"] = "text/html; charset=utf-8"
   render       :partial => 'lists'
 end

Alain


#3

On Feb 25, 2006, at 10:38, Alain R. wrote:

If you use AJAX and partial rendering, you may have to specify it
through headers, in order for special international characters to
render correctly :

def update_positions
  @lists = ...
  @headers["Content-Type"] = "text/html; charset=utf-8"
  render       :partial => 'lists'
end

Argh, I missed that detail in my AJAX actions, which are called as
components as well.

Since all my application is UTF-8 setting the header could be done
for all actions, would it be clean to set that header with a filter
in application.rb provided my program only serves XHTML? If that’s
right, is the META element still needed? Maybe it doesn’t hurt as
long as they coincide?

– fxn


#4

class ApplicationController < ActionController::Base
before_filter :set_charset
def set_charset
@headers[“Content-Type”] = “text/html; charset=iso-8859-1”
end
end

Great tip.
AFAIK, this is the only way to make in_place_edit work with special
characters, as the methods that return the partial are generated by
Rails, and we can’t access them.

How come Rails doesn’t do this by default?

Alain


#5

On 2/25/06, Alain R. removed_email_address@domain.invalid wrote:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
   @lists = ...

Just wanted to say this worked perfectly, thanks.


#6

Pat M. wrote:

I have a page which validates “tentatively,” because the validator
uses the default character encoding. None was specified apparently,
so it falls back to UTF-8. How and where do I specify the character
encoding?

This works for me

app/constrollers/application.rb:

class ApplicationController < ActionController::Base
before_filter :set_charset

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

end

// Daniel