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