-2

I'm trying to get the PaperClip params by using jQuery and create an image directly.

i did something like

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

and I passed the imageData to a ruby function like

  send_message: function(imageData) {
    return this.perform('send_message', {
      image: imageData
    });

but when I tried to create a message like

Messages.create!(image: data['image'])

it returns an error

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

No idea why imageData == {} while $('input[type=file]')[0].files[0]) returns something like

File {name: "image.png", lastModified: 1499752728000, lastModifiedDate: Tue Jul 11 2017 13:58:48 GMT+0800 (HKT), webkitRelativePath: "", size: 602802…}

I know that PaperClip requires something like this in params, but not sure how to get this from 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>>}
Brian Law
  • 460
  • 1
  • 4
  • 18
  • At the very least, you can encode them in Base64 (or something), decode on the client and show. – Sergio Tulentsev Jul 17 '17 at 05:48
  • [Here is a very clear and good answer. In short, you can, but ActionCable dont have an interface for that. Use plain POST request.](https://stackoverflow.com/a/11081396/2057388) – Roman Kiselenko Jul 17 '17 at 05:54

1 Answers1

0

I think you can use active storage https://github.com/rails/activestorage to upload image and you can display it through action cable.

Salanoid
  • 43
  • 1
  • 6