ActionCable не вещает частично из метода webhook / create? - PullRequest
0 голосов
/ 29 апреля 2020

Я настроил actioncable для работы с ответами Twilio API. Проблема, с которой я столкнулся, заключается в том, что actioncable не работает, когда ответ поступает от контроллера webhook, и я не могу отобразить html объекты, в которые включены вспомогательные методы rails. Как я могу получить actioncable для правильной трансляции на сообщение show action. Когда ответ является локальным и из моего webhook, создать действие?

Журнал Rails

Message Create (0.9ms) INSERT INTO "messages" ("conversation_id",....)
app/controllers/webhooks/twilio_controller.rb:10:in `create'
(10.5) COMMIT
app/controllers/webhooks/twilio_controller.rb:10in `create'
[ActionCable] Broadcasting to messages: {:html => "<div class=\"comment\">\n....
Completed 200 OK in 29ms

Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION:...
MessageChannel is transmitting the subscription confirmation
MessageChannel is streaming from message_channel

action cable helpers / application_helper.rb

def render_message(message)
  ApplicationController.render({partial: "messages/message",locals: {message: message}})
end

def send_cable(message)
   html = render_message(message)
   ActionCable.server.broadcast 'messages', html: html
end

messages_controller

class MessagesController < ApplicationController
  include ApplicationHelper
  def create
    @messages = @conversation.messages.build(message_params)
    if @message.save
     send_cable(@message)
    else
    # it broke
   end
  end
end

разговоров / шоу. html .erb

//boiler plate divs
..
<div id="comments">
  <%= render @conversation.messages %>
</div>
<%= render "messages/form", conversation: @conversation, message: Message.new %>

messages / create. js .erb

  $('#comments').append("<%= j(render @message) %>").hide().fadeIn(2000);
    $('#message_form')[0].reset();

message_channel. js

import consumer from "./consumer"

consumer.subscriptions.create("MessageChannel", {

 connected() {
   },
 disconnected() {
 },

 received: function(data) {
  $("#comments").append('<div class="comment">\n +
    '<div class="avatar">\n' +
    '<div class="ui label <%= data.outbound ? \'blue\' : \'green\' %> circle empty horizontal>. 
     </div>\n' +
     ....

    ').hide().fadeIn(2000);
   $('#message_form')[0].reset();
 }
});

message_channel.rb

class MessageChannel < ApplicationCable::Channel
  def subscribed
  stream_from 'message_channel'
  end

 def unsubscribed
  #
 end
end

twilio_controller.rb

class Webhooks::TwilioController < ApplicationController
  skip_before_action :verify_authenticity_token
  include ApplicationHelper

  def create
    provider = PersonalInfo.find_by(mobile_phone: params[:From])
    body = params[:Body]
    conversation = Conversation.find_or_create_by(personal_info: provider)
    message = conversation.messages.create!(personal_info: provider, body: body)
    send_cable(message) # Does not render new message when fired off
    render json: {state: 200}
  end

end

...