Forum: Ruby on Rails Setting character encoding - do I do it with Rails or lightt

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.
Pat M. (Guest)
on 2006-02-25 05:48
(Received via mailing list)
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
Alain R. (Guest)
on 2006-02-25 11:41
(Received via mailing list)
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:

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




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
Xavier N. (Guest)
on 2006-02-25 12:45
(Received via mailing list)
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
Daniel (Guest)
on 2006-02-25 14:08
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
Alain R. (Guest)
on 2006-02-25 21:40
(Received via mailing list)
> 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
Pat M. (Guest)
on 2006-02-26 03:20
(Received via mailing list)
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.
This topic is locked and can not be replied to.