Encoding error

Hello, I have in my db a register what have special characters, and when I try to put on my form to edit this values, this happens:

incompatible character encodings: UTF-8 and ASCII-8BIT

Extracted source (around line #4):

1: <%= form_for :group, :url => { :action => "update" } do |f| %> 2: <%= utf8_enforcer_tag %> 3: <label>Nome do grupo</label> <%= f.text_field :name %> <br /> 4: <label>Descrição</label> <%= f.text_area :description, :cols => 60, :rows => 6 %><br /> 5: <%= f.submit "Editar grupo" %> 6: <% end %>

In my database.yml

development:   adapter: mysql   enconding: utf8   database: mailler_development   reconect: false   username: mailler   password:   pool: 5   timeout: 5000

In my list action, I put utf8_encoding( "utf-8" ) to works, but I was searching and I don't find nothing for form_for.

thank you

Hi!

I found a typo:

enconding: utf8

Change for:

encoding: utf8

Best Regards,

Everaldo

Quoting Felipe Pieretti Umpierre <lists@ruby-forum.com>:

Hello, I have in my db a register what have special characters, and when I try to put on my form to edit this values, this happens:

incompatible character encodings: UTF-8 and ASCII-8BIT

Extracted source (around line #4):

1: <%= form_for :group, :url => { :action => "update" } do |f| %> 2: <%= utf8_enforcer_tag %> 3: <label>Nome do grupo</label> <%= f.text_field :name %> <br /> 4: <label>Descrição</label> <%= f.text_area :description, :cols => 60, :rows => 6 %><br /> 5: <%= f.submit "Editar grupo" %> 6: <% end %>

In my database.yml

development:   adapter: mysql   enconding: utf8   database: mailler_development   reconect: false   username: mailler   password:   pool: 5   timeout: 5000

In my list action, I put utf8_encoding( "utf-8" ) to works, but I was searching and I don't find nothing for form_for.

Try the mysql2 adapter. The mysql adapter always returns ASCII-8BIT strings, even if MySQL is using UTF8 encoding.

HTH,   Jeffrey

Ok, I put the encoding: utf8

development:   adapter: mysql   encoding: utf8   database: mailler_development   reconect: false   username: mailler   password:   pool: 5   timeout: 5000

so my list.html.erb when I have this line of code

<td><%= g.description.force_encoding( "utf-8" ) %></td>

so this was the text

Novamente, nenhuma descrição

But when I change, <td><%= g.description %></td>, shows again the error

incompatible character encodings: UTF-8 and ASCII-8BIT

Extracted source (around line #16):

13: <tr style="background-color: <%= cycle( "#EEE", "#DDD" ) %>"> 14: <td><%= g.id %></td> 15: <td><%= g.name %></td> 16: <td><%= g.description %></td> 17: <td> 18: <%= link_to "Editar", { :action => "edit", :id => g.id } %> <br /> 19: <%= link_to "Deletar", { :action => "destroy", :id => g.id }, :class => "delete", :confirm => "Tem certeza que deseja deletar '#{g.name}' ?" %>

Thank you

Hi!

Try to follow Jefrey tip. Because he said that mysql adapter returns ASCII-8bit.

You must use

adapter: mysql2

Regards,

Everaldo

Everaldo Gomes wrote in post #1044692:

Hi!

Try to follow Jefrey tip. Because he said that mysql adapter returns ASCII-8bit.

You must use

adapter: mysql2

Regards, Everaldo

On Tue, Feb 7, 2012 at 9:10 PM, Felipe Pieretti Umpierre <

I tried, but was the same error.

I’m sorry, I don’t have more ideas to solve this.

Are you brazilian, right?

Why don’t you try the brazilian mail list? rails-br@googlegroups.com

I suppose someone there know how to solve this issue.

Regards,

Everaldo

Quoting Felipe Pieretti Umpierre <lists@ruby-forum.com>:

Everaldo Gomes wrote in post #1044692: > Hi! > > Try to follow Jefrey tip. Because he said that mysql adapter returns > ASCII-8bit. > > You must use > > adapter: mysql2 > > Regards, > Everaldo > > On Tue, Feb 7, 2012 at 9:10 PM, Felipe Pieretti Umpierre <

I tried, but was the same error.

In the Rails console, pick a model, I use Item here, try this, and show us the result.

i = Item.first i.title # any string field will do i.title.encoding

The desired value is:

=> #<Encoding:UTF-8>

A couple of other things, I assume you are using Ruby 1.9, correct? And did you restart your server after changing database.yml?

Jeffrey

Jeffrey L. Taylor wrote in post #1044767:

Quoting Felipe Pieretti Umpierre <lists@ruby-forum.com>:

> Regards, > Everaldo > > On Tue, Feb 7, 2012 at 9:10 PM, Felipe Pieretti Umpierre <

I tried, but was the same error.

In the Rails console, pick a model, I use Item here, try this, and show us the result.

i = Item.first i.title # any string field will do i.title.encoding

The desired value is:

=> #<Encoding:UTF-8>

A couple of other things, I assume you are using Ruby 1.9, correct? And did you restart your server after changing database.yml?

Jeffrey

1.9.3-p0 :003 > group = Group.first   Group Load (0.8ms) SELECT `groups`.* FROM `groups` LIMIT 1 => #<Group id: 1, name: "Felipe", description: "ahsidaus", created_at: "2012-02-08 22:32:26", updated_at: "2012-02-08 22:32:26"> 1.9.3-p0 :004 > group.description => "ahsidaus" 1.9.3-p0 :005 > group.description.encoding => #<Encoding:ASCII-8BIT>

My encoding is different...

Quoting Felipe Pieretti Umpierre <lists@ruby-forum.com>:

Jeffrey L. Taylor wrote in post #1044767: > Quoting Felipe Pieretti Umpierre <lists@ruby-forum.com>: >> > Regards, >> > Everaldo >> > >> > On Tue, Feb 7, 2012 at 9:10 PM, Felipe Pieretti Umpierre < >> >> I tried, but was the same error. >> > > In the Rails console, pick a model, I use Item here, try this, and show > us the > result. > >>> i = Item.first >>> i.title # any string field will do >>> i.title.encoding > > The desired value is: > > => #<Encoding:UTF-8> > > A couple of other things, I assume you are using Ruby 1.9, correct? And > did > you restart your server after changing database.yml? > > Jeffrey

1.9.3-p0 :003 > group = Group.first   Group Load (0.8ms) SELECT `groups`.* FROM `groups` LIMIT 1 => #<Group id: 1, name: "Felipe", description: "ahsidaus", created_at: "2012-02-08 22:32:26", updated_at: "2012-02-08 22:32:26"> 1.9.3-p0 :004 > group.description => "ahsidaus" 1.9.3-p0 :005 > group.description.encoding => #<Encoding:ASCII-8BIT>

My encoding is different...

This is your problem. The ASCII-8BIT string from the database are conflicting with UTF-8 strings in your views. Are you sure you are using mysql2? From the Rails console:

require 'active_record' Group.first Mysql2::VERSION

=> "0.3.11"

"0.3.11" is the version of Mysql2 on my system. If it is not defined, try Mysql::VERSION. Presumably that will be defined. You will have to figure out why the wrong gem is used. At the command line "gem list mysql" will tell which are installed. Double check config/database.yml for "adapter: mysql2".

HTH,   Jeffrey

I resolved this problem, after re install my ubuntu, and start another project, I got the same error, so I put mysql2 as adapter and restart my server.

Thank you.