4

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:

  (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.

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
Yuri Gert
  • 103
  • 10
  • 1
    `\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](https://stackoverflow.com/questions/16428666/encodingundefinedconversionerror-xe4-from-ascii-8bit-to-utf-8?rq=1) helps you any. – ForeverZer0 Jul 15 '18 at 06:33
  • Possible duplicate of [carrierwave upload Encoding::UndefinedConversionError: "\xFF" from ASCII-8BIT to UTF-8](https://stackoverflow.com/questions/24164205/carrierwave-upload-encodingundefinedconversionerror-xff-from-ascii-8bit-to) – ForeverZer0 Jul 15 '18 at 07: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 '18 at 17:41
  • 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 '18 at 19:33
  • 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 '18 at 04:59
  • Not sure if this is you as well, but this looks like it's an open issue in Carrierwave [here on github](https://github.com/carrierwaveuploader/carrierwave/issues/2302). Looks like the work around at the moment is to use a string field instead of a json field like @tukan is suggesting. – trueinViso Jul 24 '18 at 23:14
  • @JeffCaros I changed my attribute to a string array instead of json and now it works. Still not sure what the cause was though. – Yuri Gert Jul 27 '18 at 03:31

1 Answers1

3

My guess would be that you need to use :string instead of :json in the 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.

tukan
  • 17,050
  • 1
  • 20
  • 48