Rails wrap_parameter无法正常工作 [英] Rails wrap_parameter not working as expected

查看:69
本文介绍了Rails wrap_parameter无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AngularJS和jQuery-file-uploader插件.我已经配置了rails来包装我的参数

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

这一切都可以正常工作,除非我尝试上传文件.我使用的上载器插件与正常使用略有不同,但它仍可以正常工作.而是让插件在添加文件时上传文件,我创建一个新记录,然后上传文件.该请求已正确触发,但是文件的参数没有被rails包裹.在我的日志中,我得到了

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"}

我希望看到参数

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

在我的MeetingsController中,我已经

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?

推荐答案

这是因为文件上传请求具有multipart/form-data格式.

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

要也以这种格式激活自动包装,您可以添加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

这篇关于Rails wrap_parameter无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆