Carrierwave: Encoding::UndefinedConversionError (“xFF” from ASCII-8BIT to UTF-8)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Carrierwave: Encoding::UndefinedConversionError (“xFF” from ASCII-8BIT to UTF-8)



I'm trying to upload multiple files to a record called Post, with a json attribute called images, using Carrierwave. But when I try to save the record, I get this error:


Post


images


(0.5ms) ROLLBACK
Completed 401 Unauthorized in 15ms (ActiveRecord: 0.8ms)
Encoding::UndefinedConversionError ("xFF" from ASCII-8BIT to UTF-8):



I've used Carrierwave before and I never got this error. I checked if anyone's had this problem, before, and I didn't find much. This question suggested the activesupport-json_encoder gem, but this gem isn't compatible with Rails 5. Also suggested were the force_encoding method and using a "wb" flag when saving the file, but the Carrierwave code has no place to implement those suggestions.


activesupport-json_encoder


force_encoding



Here's the code I have:



form


<%= form_for @post, :html => {multipart: true}, do |f| %>
<%= f.file_field :images, multiple: true %>
<%= f.submit "submit" %>
<% end %>



posts controller


...
private
def post_params
params.require(:post).permit({images: })
end
end



posts migration


...
create_table :posts do |t|
t.json :images
...



posts uploader


class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick

storage :fog

def fix_exif_rotation
manipulate! do |img|
img.tap(&:auto_orient)
end
end

process :fix_exif_rotation
process :resize_to_fit => [800, 56000]

version :thumb do
process resize_to_fit: [300, 56000]
end
end





xFF is a UTF-8 byte order marking (BOM), which is a non-printable character that gets put on the top of some files, kinda like a header so things that open them can read and know the encoding of the text before reading. Your string is not in the encoding Ruby is expecting. See if this question helps you any.
– ForeverZer0
Jul 15 at 6:33




xFF





Possible duplicate of carrierwave upload Encoding::UndefinedConversionError: "xFF" from ASCII-8BIT to UTF-8
– ForeverZer0
Jul 15 at 7:57





@ForeverZer0 Do you know where I would place force_encoding in my code? There doesn't seem to be a place to call that.
– Yuri Gert
Jul 15 at 17:41


force_encoding





I am not very familiar with CarrierWave nor Rails, but often simply opening/saving files in the correct encoding. Unless something needs to be properly encoded to be displayed as a string, opening/saving as binary with wb and rb flags will omit the need for "proper" encoding.I noticed your files may be images, if so, this would be an option.
– ForeverZer0
Jul 15 at 19:33


wb


rb





I ran into this same exact issue, I have no idea how to proceed. Please update this if you figure it out.
– Jeff Caros
Jul 21 at 4:59




1 Answer
1



My guess would be that you need to use :string instead of :json in the ActiveRecord::Migration.


:string


:json


ActiveRecord::Migration


class AddImagesToGallery < ActiveRecord::Migration
def change
add_column :galleries, :images, :string, array: true, default: # add images column as array
end
end



You can check following How to: Add more files and remove single file when using default multiple file uploads feature for more.



EDIT - added the reason



The reason why you are getting this error is that the gem activesupport-json_encoder (the one PR is hanging there from 2014) is no longer maintained and is not compatible with rails 5.x.


activesupport-json_encoder





Yes, this was it. Do you know why having the attribute as json would have caused this?
– Yuri Gert
2 days ago





@YuriGert See my edit. The reason was not a json attribute but an obsolete gem.
– tukan
yesterday






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results