我正在使用此代码上传用户的个人资料图片
if params[:user][:image].present?
uploaded_io = params[:user][:image]
name = "image_" << @user.username << uploaded_io.original_filename
File.open(Rails.root.join('public', 'images','profile',name ), 'w') do |file|
file.write(uploaded_io.read)
end
end
但它给这个错误,当我运行它,cannot convert ascii-8bit to utf-8bit
所以我所做的就是我改变了我的默认编码application.rb
文件中像这样config.encoding = "ascii-8bit"
的config.encoding = "utf-8"
。它开始正常工作。
但是问题是,现在我正在向用户发送一封邮件,以通过简单的rails mailer成功进行注册,并且现在在发送邮件之前,它给了我这个错误cannot convert utf-8bit to ascii-8bit
。
现在,我完全被其中的一部作品打动了。我尝试了很多建议的解决方案,enforce_encoding on strings
但似乎都没有用。任何帮助,将不胜感激。谢谢。
将“ w”更改为“ wb”
if params[:user][:image].present?
uploaded_io = params[:user][:image]
name = "image_" << @user.username << uploaded_io.original_filename
File.open(Rails.root.join('public', 'images','profile',name ), 'wb') do |file|
file.write(uploaded_io.read)
end
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句