Rails - 从jQuery for Paperclip获取正确的类

时间:2017-07-17 05:46:30

标签: jquery ruby-on-rails paperclip actioncable

我正在尝试使用jQuery获取PaperClip参数并直接创建图像。

我做了类似

的事情
imageData = new FormData();
imageData.append('image', $('input[type=file]')[0].files[0]);

我将imageData传递给像

这样的ruby函数
  send_message: function(imageData) {
    return this.perform('send_message', {
      image: imageData
    });

但是当我尝试创建像

这样的消息时
Messages.create!(image: data['image'])

它会返回错误

Could not execute command from ({"command"=>"message", "identifier"=>"{\"channel\":\"ConversationsChannel\", "data"=>"{\"image\":{},\"action\":\"send_message\"}"})
[Paperclip::AdapterRegistry::NoHandlerError - No handler found for {}]

imageData == {}返回类似

的内容时,不知道为什么$('input[type=file]')[0].files[0])
File {name: "image.png", lastModified: 1499752728000, lastModifiedDate: Tue Jul 11 2017 13:58:48 GMT+0800 (HKT), webkitRelativePath: "", size: 602802…}

我知道PaperClip在params中需要这样的东西,但不知道如何从jQuery中获取此内容

"asset"=>
  {"image"=>
    #<ActionDispatch::Http::UploadedFile:0x000000056679e8
    @content_type="image/jpg",
    @headers= "Content-Disposition: form-data; name=\"asset[image]\";
      filename=\"2009-11-29-133527.jpg\"\r\nContent-Type: image/jpg\r\n",
    @original_filename=""2009-11-29-133527.jpg"",
    @tempfile=#<File:/tmp/RackMultipart20120619-1043-yvc9ox>>}

1 个答案:

答案 0 :(得分:0)

我认为您可以使用有效存储https://github.com/rails/activestorage上传图片,您可以通过操作电缆显示它。