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

时间:2015-10-05 14:21:02

标签: javascript ruby-on-rails controller faye

我按照本教程在rails app中创建实时聊天:http://josephndungu.com/tutorials/gmail-like-chat-application-in-ruby-on-rails

与此示例不同,您可以点击属于该用户的按钮,并且会弹出聊天并将其保留在用户索引页面(root)上,我希望有一个"嵌入式&#34 ;聊天,所以当你转到用户显示页面和http请求时,它已经存在并准备好输入。

我怎么能这样做?目前,如果我试图嵌入应用程序说没有对话。我想原因是在呈现网站后JS被加载,所以在需要时,conversation.id还没有。我试图打电话给对话控制器为用户控制器创建操作,但我还没能把它拉下来。

以下是当前代码:

初始化对话的按钮:

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

users.js(发送数据以创建对话控制器的操作)

    $('.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);
        });
    });

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();
        },

会话控制器

  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(弹出的对话窗口)

<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) %>

(最后一行是针对private_pub gem)

0 个答案:

没有答案