我正在尝试在一对一聊天应用程序上实现private_pub,没有private_pub一切正常但不是理想的聊天应用程序,因为没有websocket用于自动更新。 我尝试在private_pub的RailsCasts上实现,但我得到模板丢失或未知格式。 这是我的代码:
路线
resources :conversations do
resources :messages
end
我的用户显示页面中有一个按钮用于开始聊天(创建/使用对话并显示对话显示页面,如facebook / gchat上的一对一聊天框,对话有很多消息):< / p>
<% unless current_user == @user %>
<%= link_to "Send Message", conversations_path(:sender_id => current_user.id , :recipient_id => @user.id ), :method => :post, class: "btn btn-success btn-xs start-conversation" %>
<% end %>
对话控制器
class ConversationsController < ApplicationController
before_filter :authenticate_user!
layout false
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
@conversation.save!
redirect_to @conversation
end
def show
@conversation = Conversation.find(params[:id])
@reciever = 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
end
Convesation Show View
<% content_for :bottom do %>
<%= subscribe_to conversation_path(@conversation) %>
<% end %>
<div class="chatboxhead">
<div class="chatboxtitle">
<i class="fa fa-comments"></i>
<h1><%= @reciever.username %> </h1>
</div>
<div class="chatboxoptions">
<%= link_to "<i class='fa fa-minus'></i> ".html_safe, "#", class: "toggleChatBox", "data-cid" => @conversation.id %>
<%= 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.reverse %>
<% end %>
</div>
<div class="chatboxinput">
<%= form_for([@conversation, @message], :remote => true) do |f| %>
<%= f.text_area :body, class: "chatboxtextarea", "data-cid" => @conversation.id %>
<%= f.submit " Send", class: "btn btn-primary btn-xs"%>
<% end %>
</div>
消息控制器
class MessagesController < ApplicationController
before_filter :authenticate_user!
skip_before_filter :verify_authenticity_token, only: [:create]
def create
@conversation = Conversation.find(params[:conversation_id])
@message = @conversation.messages.build(message_params)
@message.user_id = current_user.id
@message.save!
@path = conversation_path(@conversation)
end
private
def message_params
params.require(:message).permit(:body)
end
end
消息的消息部分
#_message.html.erb
<li class="<%= self_or_other(message) %>">
<div class="avatar">
<img src="http://placehold.it/50x50" />
</div>
<div class="chatboxmessagecontent">
<p><%= message.body %></p>
<time datetime="<%= message.created_at %>" title="<%= message.created_at.strftime("%d %b %Y at %I:%M%p") %>">
<%= message_interlocutor(message).username %> <%= message.created_at.strftime("%H:%M %p") %>
</time>
</div>
</li>
消息的创建视图
#create.js.erb
<% publish_to @path do %>
var id = "<%= @conversation.id %>";
var chatbox = $(".chatboxcontent");
var sender_id = "<%= @message.user.id %>";
var reciever_id = $('meta[name=user-id]').attr("content");
chatbox.append("<%= j render( partial: @message ) %>");
chatbox.scrollTop(chatbox[0].scrollHeight);
if (sender_id != reciever_id) {
chatBox.chatWith(id);
chatbox.children().last().removeClass("self").addClass("other");
chatbox.scrollTop(chatbox[0].scrollHeight);
chatBox.notify();
}
<% end %>
在Message Controller上,创建操作上面的代码结果为“Missing Template”,如果我将respond_to设置为:
respond_to do |format|
format.js { render 'create.js.erb' }
end
结果为“未知格式”
答案 0 :(得分:1)
如果尚未添加jquery_ujs
文件,则会发生此错误。您只包含了jquery文件。
因此,您需要在视图中手动添加这两个文件,或者在application.js
或您用于特定布局的任何其他文件中要求它们。
根据您的情况,您可以遵循第一或第二个解决方案。
第一个解决方案:
<%= javascript_include_tag :jquery, :jquery_ujs %>
在你的对话节目页面上。
第二个解决方案:
删除对话控制器上的"layout false"