0
votes

I'm getting this error, but only on production, and only when editing a specific record. It doesn't happen in development, and it doesn't happen for any other record. Not only that, but there are no UTF characters in the template, no UTF characters in en.yml, and the record also has no UTF characters either! I can view/show the record fine.

272 <158>1 2017-10-16T16:57:50.349003+00:00 host heroku router - at=info method=GET path="/users/26/edit" host=www.host.net request_id=6d8d22cc-3050-4628-85a4-f660991b4ce3 fwd="99.99.99.99" dyno=web.1 connect=4ms service=442ms status=500 bytes=1733 protocol=https
145 <190>1 2017-10-16T16:57:50.345615+00:00 host app web.1 - F, [2017-10-16T16:57:50.344992 #4] FATAL -- : [6d8d22cc-3050-4628-85a4-f660991b4ce3]   
228 <190>1 2017-10-16T16:57:50.345696+00:00 host app web.1 - F, [2017-10-16T16:57:50.345626 #4] FATAL -- : [6d8d22cc-3050-4628-85a4-f660991b4ce3] ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8):
173 <190>1 2017-10-16T16:57:50.346033+00:00 host app web.1 - F, [2017-10-16T16:57:50.345973 #4] FATAL -- : [6d8d22cc-3050-4628-85a4-f660991b4ce3]     55:           .input-group
150 <190>1 2017-10-16T16:57:50.346035+00:00 host app web.1 - [6d8d22cc-3050-4628-85a4-f660991b4ce3]     56:             - us = Country.find_by code: 'US'
237 <190>1 2017-10-16T16:57:50.346036+00:00 host app web.1 - [6d8d22cc-3050-4628-85a4-f660991b4ce3]     57:             =cf.collection_select :country_id, [us] + Country.all, :id, :name, {prompt: t('company.country')}, class: 'form-control'
146 <190>1 2017-10-16T16:57:50.346037+00:00 host app web.1 - [6d8d22cc-3050-4628-85a4-f660991b4ce3]     58:             .input-group-addon.required *
143 <190>1 2017-10-16T16:57:50.346038+00:00 host app web.1 - [6d8d22cc-3050-4628-85a4-f660991b4ce3]     59:   .card-header= t 'company.financials'
118 <190>1 2017-10-16T16:57:50.346039+00:00 host app web.1 - [6d8d22cc-3050-4628-85a4-f660991b4ce3]     60:   .card-block
119 <190>1 2017-10-16T16:57:50.346040+00:00 host app web.1 - [6d8d22cc-3050-4628-85a4-f660991b4ce3]     61:     .card-text
145 <190>1 2017-10-16T16:57:50.346087+00:00 host app web.1 - F, [2017-10-16T16:57:50.346035 #4] FATAL -- : [6d8d22cc-3050-4628-85a4-f660991b4ce3]   
259 <190>1 2017-10-16T16:57:50.346213+00:00 host app web.1 - F, [2017-10-16T16:57:50.346100 #4] FATAL -- : [6d8d22cc-3050-4628-85a4-f660991b4ce3] app/views/companies/_edit.haml:58:in `block in _app_views_companies__edit_haml___3739913582972992033_70135683358520'

The HAML is

      .input-group
        - us = Country.find_by code: 'US'
        =cf.collection_select :country_id, [us] + Country.all, :id, :name, {prompt: t('company.country')}, class: 'form-control'
        .input-group-addon.required *

Countries does not have any UTF characters except for 'Saint-Barthélemy' and 'São Tomé and Príncipe'. However I can still edit other records and list all countries fine.

I tried the solutions in

None of them worked. I also tried

  class Application < Rails::Application
    config.encoding = "utf-8"

I am using Heroku, Postgres, HAML, and Rails 5.0.6.

1

1 Answers

0
votes

I hooked up my local server to the production database and tried to edit the record. I commented out whole sections with -# until it loaded and narrowed it to .description field, which definitely wasn't what was in the error message. I used the web debugger to print out the field

>>  @user.company.description
=> "... world\xE2\x80\x99s largest ..."
>>  @user.company.description.encoding
=> #<Encoding:ASCII-8BIT>

And I saw it was not UTF-8 encoding. (But strange that it will display it in #show fine. I tracked the problem to a custom type

class Company < ApplicationRecord
  @@encrypted_fields = [:name, :phone, :address1, :address2, :ein, :bank, :url, :description]
  @@encrypted_fields.each do |ef|
    attribute ef, :encrypted

class EncryptedType < ActiveRecord::Type::Text
  # called when loading from DB
  def deserialize(value)
    value.decrypt.force_encoding('utf-8') unless value.nil?

I added .force_encoding('utf-8') after it decrypts and it works now.