Rolf Pedersen
2010-Aug-01 00:53 UTC
Problem with non-ascii characters in forms: "incompatible character encodings: UTF-8 and ASCII-8BIT"
Hi First off, I''m using Ruby 1.9.1p378 and Rails 2.3.8. I was creating a minimal application to test handling of Norwegian special characters when I bumped into this strange problem... I have a simple Car model with fields maker:string and model:string. For the controller I planned to just have an index action do all the work: class CarsController < ApplicationController def index if request.post? @car = Car.new(params[:car]) @car.save end @cars = Car.find(:all) end # def create # @car = Car.new(params[:car]) # @car.save # redirect_to :action => :index # end end The corresponding view lists all car models and displays a form to support the addition of new car models: <h1>Cars</h1> <table> <th>maker</th><th>model</th> <% @cars.each do |car| %> <tr> <td><%= car.maker %></td> <td><%= car.model %></td> <td> <% end %> </table> <% form_for(:car, :url => { :action => "index" }) do |f| %> <p> <%= f.label :maker %> <%= f.text_field :maker %> </p> <p> <%= f.label :model %> <%= f.text_field :model %> </p> <p> <%= f.submit ''Add'' %> </p> <% end %> Now, this works fine..... until I submit special characters in one of the fields. If I for example write "Dodge" and "Børnout" in the form fields, I get an error like this: Encoding::CompatibilityError in Cars#index Showing *app/views/cars/index.html.erb* where line *#19* raised: incompatible character encodings: UTF-8 and ASCII-8BIT Extracted source (around line *#19*): 16: </p> 17: <p> 18: <%= f.label :model %> 19: <%= f.text_field :model %> 20: </p> 21: <p> 22: <%= f.submit ''Add'' %> But, the entry is added correctly to the database anyway, so if I just reload http://localhost:3000/cars, I do see the new entry. OK, I thought... I''ve read quite a few places that there have been (and still are) various issues with support for Unicode in the different Ruby/Rails version combinations, so I figured that I just didn''t have the best combination for this. But then I temporarily built a new application by using generate scaffold, and it all works fine there. After some trying and failing I discovered that if I (in my original solution) changed the form_for :url option to :action => "create" and added a create action in the controller file (commented out in the above controller source), it works with special characters and all. So the only difference is that the form posts the data to the create action instead of the index action, and then it works. I just don''t get it! :o/ Anyone has an explanation to offer? Would be much appreciated! :o) Kind regards, Rolf -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Fernando Perez
2010-Aug-01 09:22 UTC
Re: Problem with non-ascii characters in forms: "incompatible character encodings: UTF-8 and ASCII-
Hahaha! Search the list, and you''ll notice that people have already fallen into this trap. Wait ''til Rails 3 AND Ruby 1.9.2 get released to jump to Rails+Ruby1.9 Stick to Rails 2.3.8 and Ruby 1.8.7 until then. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Rolf Pedersen
2010-Aug-01 16:27 UTC
Re: Problem with non-ascii characters in forms: "incompatible character encodings: UTF-8 and ASCII-8BIT"
I did search the list, and I found people have similar problems with Ruby 1.9.1, but not directly comparable to my issue. In my simple case it DOES work, as long as I use two controller actions instead of just the one... Meanwhile.. yes, do do stick to Ruby 1.8.7 until I know more :o) Best regards, Rolf Fernando Perez wrote:> Hahaha! > > Search the list, and you''ll notice that people have already fallen into > this trap. > > Wait ''til Rails 3 AND Ruby 1.9.2 get released to jump to Rails+Ruby1.9 > > Stick to Rails 2.3.8 and Ruby 1.8.7 until then. >On Sun, Aug 1, 2010 at 2:53 AM, Rolf Pedersen <rolfhsp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi > > First off, I''m using Ruby 1.9.1p378 and Rails 2.3.8. > > I was creating a minimal application to test handling of Norwegian special > characters when I bumped into this strange problem... > > I have a simple Car model with fields maker:string and model:string. > For the controller I planned to just have an index action do all the work: > class CarsController < ApplicationController > def index > if request.post? > @car = Car.new(params[:car]) > @car.save > end > @cars = Car.find(:all) > end > # def create > # @car = Car.new(params[:car]) > # @car.save > # redirect_to :action => :index > # end > end > > The corresponding view lists all car models and displays a form to support > the addition of new car models: > <h1>Cars</h1> > <table> > <th>maker</th><th>model</th> > <% @cars.each do |car| %> > <tr> > <td><%= car.maker %></td> > <td><%= car.model %></td> > <td> > <% end %> > </table> > > <% form_for(:car, :url => { :action => "index" }) do |f| %> > <p> > <%= f.label :maker %> > <%= f.text_field :maker %> > </p> > <p> > <%= f.label :model %> > <%= f.text_field :model %> > </p> > <p> > <%= f.submit ''Add'' %> > </p> > <% end %> > > Now, this works fine..... until I submit special characters in one of the > fields. > If I for example write "Dodge" and "Børnout" in the form fields, I get an > error like this: > Encoding::CompatibilityError in Cars#index > > Showing *app/views/cars/index.html.erb* where line *#19* raised: > > incompatible character encodings: UTF-8 and ASCII-8BIT > > Extracted source (around line *#19*): > > 16: </p> > 17: <p> > 18: <%= f.label :model %> > 19: <%= f.text_field :model %> > 20: </p> > 21: <p> > 22: <%= f.submit ''Add'' %> > > > But, the entry is added correctly to the database anyway, so if I just > reload http://localhost:3000/cars, I do see the new entry. > OK, I thought... I''ve read quite a few places that there have been (and > still are) various issues with support for Unicode in the different > Ruby/Rails version combinations, so I figured that I just didn''t have the > best combination for this. > But then I temporarily built a new application by using generate scaffold, > and it all works fine there. > After some trying and failing I discovered that if I (in my original > solution) changed the form_for :url option to :action => "create" and added > a create action in the controller file (commented out in the above > controller source), it works with special characters and all. > > So the only difference is that the form posts the data to the create action > instead of the index action, and then it works. > > I just don''t get it! :o/ > > Anyone has an explanation to offer? > Would be much appreciated! :o) > > Kind regards, > Rolf >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Conrad Taylor
2010-Aug-02 19:46 UTC
Re: Re: Problem with non-ascii characters in forms: "incompatible character encodings: UTF-8 and ASCII-8BIT"
On Sun, Aug 1, 2010 at 9:27 AM, Rolf Pedersen <rolfhsp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I did search the list, and I found people have similar problems with Ruby > 1.9.1, but not directly comparable to my issue. > In my simple case it DOES work, as long as I use two controller actions > instead of just the one... > > Meanwhile.. yes, do do stick to Ruby 1.8.7 until I know more :o) > > Best regards, > Rolf > > Fernando Perez wrote: > >> Hahaha! >> >> Search the list, and you''ll notice that people have already fallen into >> this trap. >> >> Wait ''til Rails 3 AND Ruby 1.9.2 get released to jump to Rails+Ruby1.9 >> >> Stick to Rails 2.3.8 and Ruby 1.8.7 until then. >> > > >I would recommend the following setup: Ruby 1.9.2 RC2 install => rvm install 1.9.2 Rails 3 RC install => gem install rails --pre MySQL 2 Adapter install => gem install mysql2 Note: The MySQL 2 gem should resolve the MySQL encoding issues that people are seeing when using the regular MySQL gem and Ruby 1.9.2. Next, if you''re planning to upgrade your application to Rails 3 and Ruby 1.9.2, I would recommend that you begin the process now so that you can provide feedback to the Rails team as well as the Ruby gem/plugin authors. Good luck and happy coding, -Conrad> On Sun, Aug 1, 2010 at 2:53 AM, Rolf Pedersen <rolfhsp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hi >> >> First off, I''m using Ruby 1.9.1p378 and Rails 2.3.8. >> >> I was creating a minimal application to test handling of Norwegian special >> characters when I bumped into this strange problem... >> >> I have a simple Car model with fields maker:string and model:string. >> For the controller I planned to just have an index action do all the work: >> class CarsController < ApplicationController >> def index >> if request.post? >> @car = Car.new(params[:car]) >> @car.save >> end >> @cars = Car.find(:all) >> end >> # def create >> # @car = Car.new(params[:car]) >> # @car.save >> # redirect_to :action => :index >> # end >> end >> >> The corresponding view lists all car models and displays a form to support >> the addition of new car models: >> <h1>Cars</h1> >> <table> >> <th>maker</th><th>model</th> >> <% @cars.each do |car| %> >> <tr> >> <td><%= car.maker %></td> >> <td><%= car.model %></td> >> <td> >> <% end %> >> </table> >> >> <% form_for(:car, :url => { :action => "index" }) do |f| %> >> <p> >> <%= f.label :maker %> >> <%= f.text_field :maker %> >> </p> >> <p> >> <%= f.label :model %> >> <%= f.text_field :model %> >> </p> >> <p> >> <%= f.submit ''Add'' %> >> </p> >> <% end %> >> >> Now, this works fine..... until I submit special characters in one of the >> fields. >> If I for example write "Dodge" and "Børnout" in the form fields, I get an >> error like this: >> Encoding::CompatibilityError in Cars#index >> >> Showing *app/views/cars/index.html.erb* where line *#19* raised: >> >> incompatible character encodings: UTF-8 and ASCII-8BIT >> >> Extracted source (around line *#19*): >> >> 16: </p> >> 17: <p> >> 18: <%= f.label :model %> >> 19: <%= f.text_field :model %> >> 20: </p> >> 21: <p> >> 22: <%= f.submit ''Add'' %> >> >> >> But, the entry is added correctly to the database anyway, so if I just >> reload http://localhost:3000/cars, I do see the new entry. >> OK, I thought... I''ve read quite a few places that there have been (and >> still are) various issues with support for Unicode in the different >> Ruby/Rails version combinations, so I figured that I just didn''t have the >> best combination for this. >> But then I temporarily built a new application by using generate scaffold, >> and it all works fine there. >> After some trying and failing I discovered that if I (in my original >> solution) changed the form_for :url option to :action => "create" and added >> a create action in the controller file (commented out in the above >> controller source), it works with special characters and all. >> >> So the only difference is that the form posts the data to the create >> action instead of the index action, and then it works. >> >> I just don''t get it! :o/ >> >> Anyone has an explanation to offer? >> Would be much appreciated! :o) >> >> Kind regards, >> Rolf >> > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.