我在rails项目中设置了一个通道,用于处理传入的post请求并更新我创建的表。我有一个表字段,我想在其中替换文本,并输入来自发布请求的新文本。当我使用$('#'(.append((时,我会收到所有请求,但是,当我使用美元('#''(.replaceWith((时它会更新一次文本,但不会再次更新。replaceWith不是最佳方法吗?
// Called when there's incoming data on the websocket for this channel
received(data) {
// will only change text once. example: post1 /*will not change it to later post data */
$('#my-partial-reference').replaceWith(data.content)
// will post all updates, but in continuous string ex : post1post2post3post4
// $('#my-partial-reference').append(data.content)
}
这是我的_partial.html.erb:
<td id ="my-partial-reference"><%= call.callstatus %></td>
所以我完成了这项工作,结果有点出乎意料。
我通过输入一个分部作为我的数据流的数据来实现它。
# first i created a method to accept the partial
def post_recieved
ActionCable.server.broadcast "call_channel",
content: message_render(params["CallStatus"])
end
#here is a method I made to enter the data to my channel as a partial
def message_render(message)
my_model_ref = MyModel.first
my_model_ref.callstatus = message
#here i render my partial, but use the data from my temporary model
render(partial: 'view_sub_folder/my-partial-reference', locals: {call: my_model_ref })
end
然后在我的频道中,我用新的替换旧的部分
received(data) {
$('#my-partial-reference').replaceWith(data.content)
}
据我所知,这不应该奏效。但我的表格是实时更新的,不会错过一个电话后,有人知道为什么吗?我有一些猜测,但我远不是铁路方面的专业人士。