Rails wrap_parameter not working as expected

Deadly 提交于 2019-12-06 08:15:27

问题


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:


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


来源:https://stackoverflow.com/questions/16380893/rails-wrap-parameter-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!