没有UTF字符的HAML模板错误:ActionView :: Template :: Error(不兼容的字符编码:ASCII-8BIT和UTF-8)
问题描述:
我得到这个错误,但只在生产中,只有在编辑特定记录时。它不会在开发过程中发生,并且不会发生任何其他记录。不仅如此,模板中没有UTF字符,en.yml
中也没有UTF字符,并且该记录也没有UTF字符!我可以查看/显示罚款。没有UTF字符的HAML模板错误:ActionView :: Template :: Error(不兼容的字符编码:ASCII-8BIT和UTF-8)
272 <158>1 2017-10-16T16:57:50.349003+00:00 host heroku router - at=info method=GET path="https://*.com/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'
的HAML是
.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 *
国家没有任何UTF字符,除了“圣巴泰勒米”和“圣多美和普林西比”。不过,我仍然可以编辑其他记录并列出所有国家的罚款。
我试图解决方案
-
ActionView::Template::Error (incompatible character encodings: UTF-8 and ASCII-8BIT)(我不使用
request
。) - incompatible character encodings: ASCII-8BIT and UTF-8(我在模板中没有UTF)
- Error: Incompatible character encodings: UTF-8 and ASCII-8BIT(我设置NLS_LANG = AMERICAN_AMERICA。 UTF8。)
他们都没有工作。我也试过
class Application < Rails::Application
config.encoding = "utf-8"
我使用的是Heroku,Postgres,HAML和Rails 5.0.6。
答
我把我的本地服务器连接到生产数据库并试图编辑记录。我用-#
注释了整个部分,直到它加载并将其缩小到.description
字段,这肯定不是错误消息中的内容。我用网络调试器打印出该字段
>> @user.company.description
=> "... world\xE2\x80\x99s largest ..."
>> @user.company.description.encoding
=> #<Encoding:ASCII-8BIT>
而我看到它不是UTF-8编码。 (但奇怪的是,它会在#show
精细显示。我跟踪问题的自定义类型
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?
我加入.force_encoding('utf-8')
它解密和现在的工作之后。