2017-01-23 61 views
0

我想用我的聊天應用程序附加圖像。我已經完成了文字聊天,但我無法通過actioncable發送附件。ActionCable with attachment

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 

回答

0

我不能評論,但我可以給你一個提示來完成它。

正如你所知道的軌道form_for不支持ajax上傳,它是非常困難的軌道ajax上傳我不得不深入挖掘。

首先安裝這種寶石

gem 'remotipart', '~> 1.2' 

加入您可以創建文件輸入的表單寶石後。最好的辦法是在添加一個gem文件後自動上傳。 創建表單時務必在form_for處添加remote: true

我已經試過用回形針寶石,它工作得很好。如果你有問題,然後ping我們