Javascript rails与private_pub聊天(来自其他控制器的控制器操作,JS到rails)

Javascript rails与private_pub聊天(来自其他控制器的控制器操作,JS到rails),javascript,ruby-on-rails,controller,faye,Javascript,Ruby On Rails,Controller,Faye,我将按照本教程创建rails实时聊天应用程序: 与此示例不同,您可以单击属于用户的按钮,聊天将弹出,并停留在用户索引页(root)上,我希望有一个“嵌入式”聊天,因此当您使用http请求转到用户显示页时,它将已经在那里并准备好输入 我怎么能这么做?目前,如果我尝试嵌入该应用程序,它会说没有conversation.id。我想原因是JS是在站点呈现后加载的,所以当需要时conversation.id还没有出现。我试图调用conversations控制器为users控制器创建的操作,但未能成功 以下

我将按照本教程创建rails实时聊天应用程序:

与此示例不同,您可以单击属于用户的按钮,聊天将弹出,并停留在用户索引页(root)上,我希望有一个“嵌入式”聊天,因此当您使用http请求转到用户显示页时,它将已经在那里并准备好输入

我怎么能这么做?目前,如果我尝试嵌入该应用程序,它会说没有conversation.id。我想原因是JS是在站点呈现后加载的,所以当需要时conversation.id还没有出现。我试图调用conversations控制器为users控制器创建的操作,但未能成功

以下是当前代码:

初始化对话的按钮:

    <%= link_to "Send message", "#", class: "btn btn-success btn-xs start-conversation", "data-sid" => current_user.id, "data-rip" => @user.id %>
chat.js

chatBox = {

        /**
         * creates an inline chatbox on the page by calling the
         * createChatBox function passing along the unique conversation_id
         * 
         * @param conversation_id
         */

        chatWith: function (conversation_id) {

            chatBox.createChatBox(conversation_id);
            $("#chatbox_" + conversation_id + " .chatboxtextarea").focus();
        },
对话控制器

    $('.start-conversation').click(function (e) {
        e.preventDefault();

        var sender_id = $(this).data('sid');
        var recipient_id = $(this).data('rip');

        $.post("/conversations", { sender_id: sender_id, recipient_id: recipient_id }, function (data) {
            chatBox.chatWith(data.conversation_id);
        });
    });
  def create
    if Conversation.between(params[:sender_id], params[:recipient_id]).present?
      @conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
    else
      @conversation = Conversation.create!(conversation_params)
    end

    render json: { conversation_id: @conversation.id }
  end

  def show
    @conversation = Conversation.find(params[:id])
    @receiver = interlocutor(@conversation)
    @messages = @conversation.messages
    @message = Message.new
  end

  private

  def conversation_params
    params.permit(:sender_id, :recipient_id)
  end

  def interlocutor(conversation)
    current_user == conversation.recipient ? conversation.sender : conversation.recipient
  end
show.html.erb(弹出的对话窗口)


@会话id%>
@会话id%>

true,:html=>{id:“对话形式{@conversation.id}})do | f |%> @会话id%>
(最后一行是针对private_pub gem的)

<div class="chatboxhead">
  <div class="chatboxtitle">
    <i class="fa fa-comments"></i>

    <h1><%= @receiver.profile.first_name %> <%= @receiver.profile.last_name %></h1>
  </div>
  <div class="chatboxoptions">
    <%= link_to "<i class='fa  fa-minus'></i> ".html_safe, "#", class: "toggleChatBox", "data-cid" => @conversation.id %>
    &nbsp;&nbsp;
    <%= link_to "<i class='fa  fa-times'></i> ".html_safe, "#", class: "closeChat", "data-cid" => @conversation.id %>
  </div>
  <br clear="all"/>
</div>
<div class="chatboxcontent">
  <% if @messages.any? %>
      <%= render @messages %>
  <% end %>
</div>
<div class="chatboxinput">
  <%= form_for([@conversation, @message], :remote => true, :html => {id: "conversation_form_#{@conversation.id}"}) do |f| %>
      <%= f.text_area :body, class: "chatboxtextarea", "data-cid" => @conversation.id %>
  <% end %>
</div>
<%= subscribe_to conversation_path(@conversation) %>