我有完美的发布消息的方法:
def new_message
#adding message to database
tmp_message = Message.new
# Check if user is muted
if current_user.role == 'muted'
params[:message] = "пытался что-то сказать, но кляп оказался сильнее"
end
# Check if the message is private
if recipient = params[:message].match(/@(.+) (.+)/)
# It is private, send it to the recipient's private channel
@channel = "/messages/private/#{recipient.captures.first}"
@message = { :username => current_user.username, :msg => recipient.captures.second }
else
# It's public, so send it to the public channel
@channel = "/messages/public"
@message = { :username => current_user.username, :msg => params[:message] }
end
tmp_message.channel = @channel
tmp_message.message_text = params[:message]
tmp_message.username = current_user.username
tmp_message.save
respond_to do |f|
f.js
end
end
然后,我复制了这个方法来处理来自数据库的数据:
def get_messages
for i in 1..Message.count
tmp_message = Message.find_by_id(i)
if tmp_message
if recipient = tmp_message.message_text.match(/@(.+) (.+)/)
# It is private, send it to the recipient's private channel
@channel = "/messages/private/#{recipient.captures.first}"
@message = { :username => tmp_message.username, :msg => recipient.captures.second }
else
# It's public, so send it to the public channel
@channel = "/messages/public"
@message = { :username => tmp_message.username, :msg => tmp_message.message_text }
end
end
end
respond_to do |format|
format.js { render nothing: true }
end
端
并在" room"方法:
def room
redirect_to sign_up_path unless user_signed_in?
if current_user.email == "madowley@gmail.com"
current_user.role = "admin"
end
@queue = Array.new;
@queue = queue_list
get_messages
end
get_messages.js看起来像:
// Clear message input
$('#message').val('');
// Send the message
<% publish_to @channel, @message %>
我试图添加&#34; format.html&#34;得到&#34;跳转错误,丢失块(产量)&#34;
答案 0 :(得分:0)
很难测试您的解决方案。但是,我认为问题在于你要做的事情的渲染机制......还有一些需要考虑的事情:
null
)为了解决这个问题,我认为你的控制器的一些重组应该是有序的:
var clientCollection = new Mongo.Collection(null);
正如我所说,没有测试过......让我们希望它有用。