How to define content charset?


#1

I tried to define koi8-r charset in app/views/layouts/ like this:

Cpu: <%= controller.action_name %> <%= stylesheet_link_tag 'scaffold' %>

But it doesn’t work. I suppose WEBrick already sent utf8 charset in it’s
header, didn’t it?
How can I do the subject?


#2

You need to add this to the response headers, either in specific actions
like this:

def list
	@response.headers["Content-Type"] = "text/html;

charset=koi8-r"
end

Or in a before filter that you apply to as many actions as you want:

class FooController < ApplicationController
  before_filter :set_content_type

  def set_content_type
    @response.headers["Content-Type"] = "text/html; charset=koi8-r"
  end
end

Note that Rails built in Action Cache will break your code if you use it

Rails will send the default charset for cached requests, and Page
Caching
may not work either.


#3

On Wed, 25 Jan 2006 12:11:00 -0800
“Tom F.” removed_email_address@domain.invalid wrote:

Tom, thanks for the answer, it works very well.

Note that Rails built in Action Cache will break your code if you use it -
Rails will send the default charset for cached requests, and Page Caching
may not work either.
I don’t use caching at this moment, but I can’t understand why caching
will break it?