When trying to remotely upload an image via Carrierwave and Rails 5, I'm presented with an encoding error:
Encoding::UndefinedConversionError - "\x89" from ASCII-8BIT to UTF-8
I have looked at this answer as well as the activesupport-json_encoder gem suggested by the answer, but no luck. The gem does not support rails 5 yet it would seem.
My form is a normal multipart rails form that looks like this:
= bootstrap_form_for @image, url: members_profile_path(current_user), remote: true, html: { multipart: true } do |f|
= f.file_field :attachment
= f.button "Save"
The field that contains the image, is called 'attachment'. In my controller my code looks like this:
def create_profile_image
respond_to do |format|
@image.entity_id = current_user.id
if @image.validate(params[ :image ])
@image.save
format.json { render json: @image, status: :ok }
else
format.json { render json: @image.errors, status: :unprocessable_entity }
end
end
end
I'm using the reform gem to handle permitted params. I have made sure to allow the attachment param. Furthermore, I have also added the Remotipart gem to simplify remote file uploads via Rails.
Any ideas why I might be presented with this error?
UPDATE
This is what I can see in my log:
Started POST "/api/v1/private/members/profiles/94/create_profile_image" for 127.0.0.1 at 2016-09-17 17:58:06 +0200
Processing by Api::V1::Private::Members::ProfilesController#create_profile_image as JSON
Parameters: {"utf8"=>"✓", "image"=>{"attachment_cache"=>"", "attachment"=>#<ActionDispatch::Http::UploadedFile:0x007fce2adb25b8 @tempfile=#<Tempfile:/var/folders/f0/7p7mh8fj2rj18b49ysfvkgb40000gn/T/RackMultipart20160917-2713-rey3nb.png>, @original_filename="imageedit_3_8650415599.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"image[attachment]\"; filename=\"imageedit_3_8650415599.png\"\r\nContent-Type: image/png\r\n">}, "button"=>"", "remotipart_submitted"=>"true", "authenticity_token"=>"f7M86L+Xisve4pNwcniTy3QaUWakL0sMtODLDkEf6Q7kGXZGCHY3rbPYe0jzPFIVjS/k0Gv4M2csoAQ7cuzMgQ==", "X-Requested-With"=>"IFrame", "X-Http-Accept"=>"application/json, text/javascript, */*; q=0.01", "id"=>"94"}
[1m[36mUser Load (1.3ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2[0m [["id", 94], ["LIMIT", 1]]
[1m[36mUser Load (0.3ms)[0m [1m[34mSELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2[0m [["id", 94], ["LIMIT", 1]]
[1m[35m (1.1ms)[0m [1m[34mSELECT COUNT(*) FROM "roles" INNER JOIN "users_roles" ON "roles"."id" = "users_roles"."role_id" WHERE "users_roles"."user_id" = $1 AND (((roles.name = 'super_admin') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)) OR ((roles.name = 'company') AND (roles.resource_type IS NULL) AND (roles.resource_id IS NULL)))[0m [["user_id", 94]]
[1m[35m (0.3ms)[0m [1m[35mBEGIN[0m
[1m[35m (0.2ms)[0m [1m[31mROLLBACK[0m
Completed 500 Internal Server Error in 31ms (ActiveRecord: 3.1ms)
Encoding::UndefinedConversionError - "\x89" from ASCII-8BIT to UTF-8:
activesupport (5.0.0) lib/active_support/core_ext/object/json.rb:34:in `encode'
activesupport (5.0.0) lib/active_support/core_ext/object/json.rb:34:in `to_json'
activesupport (5.0.0) lib/active_support/core_ext/object/json.rb:34:in `to_json'
activesupport (5.0.0) lib/active_support/json/encoding.rb:55:in `to_json'
/Users/hermannharris/.rbenv/versions/2.3.1/lib/ruby/2.3.0/json/common.rb:224:in `generate'
/Users/hermannharris/.rbenv/versions/2.3.1/lib/ruby/2.3.0/json/common.rb:224:in `generate'
activesupport (5.0.0) lib/active_support/json/encoding.rb:99:in `stringify'
activesupport (5.0.0) lib/active_support/json/encoding.rb:33:in `encode'
activesupport (5.0.0) lib/active_support/json/encoding.rb:20:in `encode'
activesupport (5.0.0) lib/active_support/core_ext/object/json.rb:37:in `to_json'
actionpack (5.0.0) lib/action_controller/metal/renderers.rb:159:in `block in <module:Renderers>'
actionpack (5.0.0) lib/action_controller/metal/renderers.rb:152:in `block in _render_to_body_with_renderer'
/Users/hermannharris/.rbenv/versions/2.3.1/lib/ruby/2.3.0/set.rb:306:in `each_key'
/Users/hermannharris/.rbenv/versions/2.3.1/lib/ruby/2.3.0/set.rb:306:in `each'
actionpack (5.0.0) lib/action_controller/metal/renderers.rb:148:in `_render_to_body_with_renderer'
actionpack (5.0.0) lib/action_controller/metal/renderers.rb:144:in `render_to_body'
actionpack (5.0.0) lib/abstract_controller/rendering.rb:26:in `render'
actionpack (5.0.0) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (5.0.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/hermannharris/.rbenv/versions/2.3.1/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
activesupport (5.0.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (5.0.0) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:43:in `render'
meta-tags (2.2.0) lib/meta_tags/controller_helper.rb:20:in `render'
app/controllers/api/v1/private/members/profiles_controller.rb:58:in `block (2 levels) in create_profile_image'
actionpack (5.0.0) lib/action_controller/metal/mime_responds.rb:201:in `respond_to'
app/controllers/api/v1/private/members/profiles_controller.rb:54:in `create_profile_image'
actionpack (5.0.0) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.0.0) lib/abstract_controller/base.rb:188:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.0.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.0.0) lib/active_support/callbacks.rb:126:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:506:in `block (2 levels) in compile'
activesupport (5.0.0) lib/active_support/callbacks.rb:455:in `call'
activesupport (5.0.0) lib/active_support/callbacks.rb:101:in `__run_callbacks__'
activesupport (5.0.0) lib/active_support/callbacks.rb:750:in `_run_process_action_callbacks'
activesupport (5.0.0) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.0.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (5.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.0.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (5.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.0.0) lib/action_controller/metal/params_wrapper.rb:248:in `process_action'
activerecord (5.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (5.0.0) lib/abstract_controller/base.rb:126:in `process'
actionview (5.0.0) lib/action_view/rendering.rb:30:in `process'
actionpack (5.0.0) lib/action_controller/metal.rb:190:in `dispatch'
actionpack (5.0.0) lib/action_controller/metal.rb:262:in `dispatch'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:50:in `dispatch'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:32:in `serve'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:39:in `block in serve'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:26:in `each'
actionpack (5.0.0) lib/action_dispatch/journey/router.rb:26:in `serve'
actionpack (5.0.0) lib/action_dispatch/routing/route_set.rb:725:in `call'
meta_request (0.4.0) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
meta_request (0.4.0) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
warden (1.2.6) lib/warden/manager.rb:34:in `catch'
warden (1.2.6) lib/warden/manager.rb:34:in `call'
rack (2.0.1) lib/rack/etag.rb:25:in `call'
rack (2.0.1) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.1) lib/rack/head.rb:12:in `call'
() Users/hermannharris/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/remotipart-7f7989db5729/lib/remotipart/middleware.rb:32:in `call'
rack (2.0.1) lib/rack/session/abstract/id.rb:222:in `context'
rack (2.0.1) lib/rack/session/abstract/id.rb:216:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.0.0) lib/active_record/migration.rb:552:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/callbacks.rb:38:in `block in call'
activesupport (5.0.0) lib/active_support/callbacks.rb:97:in `__run_callbacks__'
activesupport (5.0.0) lib/active_support/callbacks.rb:750:in `_run_call_callbacks'
activesupport (5.0.0) lib/active_support/callbacks.rb:90:in `run_callbacks'
actionpack (5.0.0) lib/action_dispatch/middleware/callbacks.rb:36:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:84:in `protected_app_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:79:in `better_errors_call'
better_errors (2.1.1) lib/better_errors/middleware.rb:57:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:49:in `call'
rack-contrib (1.2.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.4.0) lib/meta_request/middlewares/headers.rb:16:in `call'
web-console (3.3.1) lib/web_console/middleware.rb:131:in `call_app'
web-console (3.3.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.3.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.3.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.0.0) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.0.0) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:70:in `block in tagged'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.0.0) lib/active_support/tagged_logging.rb:70:in `tagged'
railties (5.0.0) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.1.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
request_store (1.3.1) lib/request_store/middleware.rb:9:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/request_id.rb:24:in `call'
rack (2.0.1) lib/rack/method_override.rb:22:in `call'
rack (2.0.1) lib/rack/runtime.rb:22:in `call'
activesupport (5.0.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.0.0) lib/action_dispatch/middleware/static.rb:136:in `call'
rack (2.0.1) lib/rack/sendfile.rb:111:in `call'
rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'
railties (5.0.0) lib/rails/engine.rb:522:in `call'
puma (3.6.0) lib/puma/configuration.rb:225:in `call'
puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
puma (3.6.0) lib/puma/server.rb:415:in `process_client'
puma (3.6.0) lib/puma/server.rb:275:in `block in run'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'
This gem provides a simple and extremely flexible way to upload files from Ruby applications. It works well with Rack based web applications, such as Ruby on Rails.
A file can be uploaded to server in two ways, one we can send the image as base64 string as plain text and another one is using multipart/form-data . The first one is the worst idea as it sends the entire image as plain text.
You are trying to render
an image to json
, which is not supported.
You probably want to customize as_json
to exclude the image (the attachment
field) from the representation of the object, or use
format.json { render json: @image, except: :attachment, status: :ok }
See this answer for more details on how to exclude a field from the json
representation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With