Rails wrap_parameter not working as expected

2019-04-12 10:00发布

I'm working with AngularJS and the jQuery-file-uploader plugin. I've configured rails to wrap my parameters with

ActiveSupport.on_load(:action_controller) do
    wrap_parameters format: [:json]
end

This is working fine for everything except when I try and upload my files. I'm using the uploader plugin slightly differently to normal but it should still be working. Rather the letting the plugin upload files when they're added, I'm creating a new record, and THEN uploading the files. The request is firing correctly, however the parameter(s) for the file(s) is/aren't being wrapped by rails. In my logs I am getting

Processing by MeetingsController#update as JSON
Parameters: {"icon"=>#<ActionDispatch::Http::UploadedFile:0x007fde79178b58 @original_filename="006.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"icon\"; filename=\"006.png\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/_v/qsm9g7nn00s0jmfkynmvwp140000gn/T/RackMultipart20130505-15753-17ig2it>>, "id"=>"35"}

I'm expecting to see the parameter being

{ :meeting => { :icon => ... }}

In my MeetingsController I've got

wrap_parameters :meeting, include: [..., :icon, ...]

The record creation which also goes through this controller works perfect, and the parameters are being wrapped as expected, however it won't work for this. Am I doing something wrong?

1条回答
做个烂人
2楼-- · 2019-04-12 10:47

That's because the file upload request has the multipart/form-data format.

To activate the autowrappring too in this format, you can add the format option :

class  MeetingsController  < ApplicationController
  wrap_parameters :meeting, include: [..., :icon, ...], format: [:json, :multipart_form]

  ...
end
查看更多
登录 后发表回答