0

I want to attach an image with my chat application. I have done with text chat but I am not able to send an attachment with actioncable.

App.global_chat = App.cable.subscriptions.create {
      channel: "ChatRoomsChannel"
      chat_room_id: messages.data('chat-room-id')
      },
      connected: ->
        # Called when the subscription is ready for use on the server

      disconnected: ->
        # Called when the subscription has been terminated by the server

      received: (data) ->
        messages.append data['message']
        messages.append data['attachment']
        messages_to_bottom()

      send_message: (message, chat_room_id, attachment) ->
        @perform 'send_message', message: message, chat_room_id: chat_room_id, attachment: attachment

    $('#new_message').submit (e) ->
      $this = $(this)
      textarea = $this.find('#message_body')
      attachment = $this.find('#message_attachment')
      if $.trim(textarea.val()).length > 1
        App.global_chat.send_message textarea.val(), messages.data('chat-room-id'), attachment[0].files[0]
        textarea.val('')
        attachment.val('')
      e.preventDefault()
      return false

1 Answers1

0

I cannot comment but I can give you a tips to accomplish it .

As you know rails form_for don't support the ajax upload and it is very hard in rails for ajax upload i had to dig in too deep.

First Install this gem

gem 'remotipart', '~> 1.2'

After adding the gem you can create a form with file input . The best thing is after adding a gem the file get upload automatically. Be Sure to add remote: true at form_for while creating forms.

I have tried with paperclip gem and it is working perfectly fine . If you problem then ping us