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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.