[英]Rails and Heroku : ArgumentError (invalid byte sequence in UTF-8) in method
I have this error : 我有这个错误:
ArgumentError (invalid byte sequence in UTF-8):
2017-02-07T14:20:10.477043+00:00 app[web.1]: app/controllers/schools_controller.rb:15:in `show'
in heroku logs in production. 在heroku中登录生产。 And the browser doesn't load the page, I have the message "something went wrong", and I don't understand where is the problem.
而且浏览器无法加载页面,我收到消息“出了点问题”,而且我不知道问题出在哪里。
It happened on a page called school, which display the 3 nearest premium school thanks to geocoder. 它发生在称为学校的页面上,该页面显示了最近的3个高级地理学校,这要归功于地理编码器。 But it doesn't happened on all the schools page, so it's really weird.
但这并不是在所有学校页面上都发生过,因此确实很奇怪。 In the admin, I can see that the schools with problem have this entry :
在管理员中,我可以看到有问题的学校有此条目:
- !ruby/hash:BSON::Document
raw_data: !binary |-
and schools without problem : 和没有问题的学校:
- !ruby/object:BSON::ObjectId
raw_data: !binary |-
Here is the task that I launch for geocode premium school and insert in mongo db : 这是我为地理编码高级学校启动的任务,并将其插入mongo db中:
namespace :geocodeschool do
desc "Show premium school near non-premium school and update them"
task :schgc => :environment do
@schools = School.all
def premium_school_aside(school)
radius = 50
@schools_a = School.near(school.coordinates.reverse, radius, units: :km)
@schools_premium = @schools_a.premium_school.limit(3)
end
@schools.each do |school|
premium_school_aside(school)
puts "// -------------- //"
puts "AUTO-ÉCOLE : #{school.title}"
puts "// -------------- //"
puts "les auto-écoles premiums près de #{school.title} : #{@schools_premium.count}"
puts "-------"
if school.school_premium_asides.blank?
school.push(school_premium_asides: @schools_premium.map(&:id))
else
school.update_attributes(school_premium_asides: @schools_premium.map(&:id))
end
puts "ICI POUR VOIR : #{@schools_premium.map(&:title)}"
school.save
end
end
end
(the field where I insert data is an Array) (我插入数据的字段是一个数组)
And here is the line which is mentionned in the error message : 这是错误消息中提到的行:
@schools_premium_aside = School.where(:id.in => @school.school_premium_asides)
I'm not sure that is due to the french text and accent, because many schools have accents and special characters and they are well displaying .. 我不确定这是由于法语和重音引起的,因为许多学校都有重音和特殊字符,并且显示效果很好。
Someone could help me to understand where is the problem ? 有人可以帮助我了解问题出在哪里? (sorry for the english, I'm French)
(对不起,英语,我是法语)
Looks like you are working in French, so I expect your user data includes the French names of schools, some of which have accented characters. 看来您使用的是法语,所以我希望您的用户数据包括学校的法语名称,其中一些带有重音字符。 If these are not encoded as UTF-8 in the DB then you will get this error when trying to display them.
如果在数据库中未将这些编码为UTF-8,则在尝试显示它们时会出现此错误。 This would explain why one page has problems and another doesn't.
这可以解释为什么一页出现问题而另一页没有问题。
You may need to check the encoding that MongoDB is using and tell Rails to expect that encoding eg by putting the following in environment.rb
您可能需要检查MongoDB使用的编码,并告诉Rails期望该编码,例如,将以下内容放入
environment.rb
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.