I am having an issue while trying to replicate Michael Hartl ActionCable tutorial https://www.learnenough.com/action-cable-tutorial (I have no knowledge in Coffeescript nor much in JS)
Especially section 4.3 when author is altering app/assets/javascripts/channels/room.coffee
in order to empty input field after the new message has been created and appended to correct DIV.
App.room = App.cable.subscriptions.create "RoomChannel",
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-table').append '<div class="message">' +
'<div class="message-user">' + data.username + ":" + '</div>' +
'<div class="message-content">' + data.content + '</div>' + '</div>'
$(document).on 'turbolinks:load', ->
submit_message()
submit_message = () ->
$('#message_content').on 'keydown', (event) ->
if event.keyCode is 13
console.log(event)
I am trying to tweak this code for my very app. Here is my attempt:
App.conversationsdevis = App.cable.subscriptions.create "ConversationsdevisChannel",
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) ->
$('#cellmessagesde'+data.cellid).append '<div class="'+data.auteur+'">'+'<span>'+data.message+'</span>'+'</div>'
$(document).on 'turbolinks:load', ->
submit_message()
submit_message = () ->
$('#cellenvoi'+data.cellid).on 'keydown', (event) ->
if event.keyCode is 13
$('input').click()
event.target.value = ""
event.preventDefault()
It's pretty much the same except that it seems that my second call to data.cellid
is not taken in.
My app has multiple chat rooms on the same page, therefore I have to select the right ID. I do this dynamically through data.cellid
I tried to move the last bits of code below received: (data) ->
but still doesn't work
EDIT EDIT EDIT
data is comming from the controller for the chatrooms messages called devimessages
Here is the create action :
def create
unless params[:devimessage][:message].empty? then
@message = @devimessageable.devimessages.new(devimessage_params)
if @message.save
ActionCable.server.broadcast 'conversationsdevis_channel', message: @message.message, auteur: @message.auteur, cellid: @message.devimessageable_type.to_s+@message.devimessageable_id.to_s
end
end
end
There are three methods to data
: message, auteur (author) and cellid (which return a string that is matching the correct ID)
Also for understanding: @devimessageable is inherited from a private function that recover polymorphic association type and ID (because the messages belong to different chatrooms of different models, I name the DIV containing the messages according to their model name and Id in my view)