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:
Cars
<% @cars.each do |car| %>maker | model | |
---|---|---|
<%= car.maker %> | <%= car.model %> | <% end %> |
<% form_for(:car, :url => { :action => “index” }) do |f| %>
<%= f.label :maker %> <%= f.text_field :maker %>
<%= f.label :model %> <%= f.text_field :model %>
<%= f.submit 'Add' %>
<% 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:
17:
18: <%= f.label :model %>
19: <%= f.text_field :model %>
20:
21:
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