7

尝试通过 Carrierwave 和 Rails 5 远程上传图像时,出现编码错误:

Encoding::UndefinedConversionError - "\x89" 从 ASCII-8BIT 到 UTF-8

我已经查看了这个答案以及答案建议的activesupport-json_encoder gem,但没有运气。gem 似乎不支持 rails 5。

我的表单是一个普通的多部分 rails 表单,如下所示:

= bootstrap_form_for @image, url: members_profile_path(current_user), remote: true, html: {  multipart: true } do |f|  
  = f.file_field :attachment
  = f.button "Save"

包含图像的字段称为“附件”。在我的控制器中,我的代码如下所示:

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

我正在使用改革 gem来处理允许的参数。我确保允许附件参数。此外,我还添加了Remotipart gem来简化通过 Rails 远程文件上传。

任何想法为什么我可能会遇到此错误?

更新

这是我在日志中可以看到的:

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'
4

4 回答 4

1

您正在尝试将render图像转换json为不支持的图像。

您可能希望自定义as_json以从对象的表示中排除图像(attachment字段),或使用

format.json { render json: @image, except: :attachment, status: :ok }

有关如何从表示中排除字段的更多详细信息,请参阅此答案json

于 2016-09-21T01:36:30.840 回答
0

bson您的Gemfile.

它可能会帮助你。

于 2016-09-17T04:48:54.480 回答
0

使用force_encoding("UTF-8")将 params[:image] 编码为 UTF-8

于 2016-09-20T11:24:20.950 回答
0

您可以尝试从支持 rails 5 的 github fork 安装 activesupport-json_encoder gem。

gem 'activesupport-json_encoder', git:'https://github.com/kongregate/activesupport-json_encoder.git', branch: 'rails-5.0'
于 2016-09-19T05:07:05.103 回答